2

I have a class that extends Application class Here is My Application class

public class UILApplication extends Application {
public int count_group;
public LoginUserInfo loginUserInfo = new LoginUserInfo();
public  static ArrayList<PostCategory> featuredCategory = new ArrayList<PostCategory>();
public ItemsAllPost selectedPost;

public PostCategory selectedCategory;
public PostCategory selectedSubCategory;
public ArrayList<PostCategory> categoryList = new ArrayList<PostCategory>();
public ArrayList<PostCategory> searchCategoryList = new ArrayList<PostCategory>();
public ArrayList<PostCategory> timePeriod = new ArrayList<PostCategory>();
public ArrayList<PostCategory> tagsList = new ArrayList<PostCategory>();
public ArrayList<ItemsAllPost> featuredPost = new ArrayList<ItemsAllPost>();
public ArrayList<ItemsAllPost> searchList = new ArrayList<ItemsAllPost>();

}

I have saved lot of data in it and using it in different Activity and Fragments

Now problem is When i press home button and come back to app after long time then it crashes.

Can anyone tell me how do i save this data when leaving the application and retain its state when i am back in application.

Please help.

Android
  • 1,085
  • 4
  • 13
  • 28
  • Read this http://developer.android.com/training/basics/activity-lifecycle/recreating.html – Dreo Aug 18 '15 at 18:27

1 Answers1

2

You generally should not put data in the Application class. The lifetime of the Application instance is different from what most people assume, and you have no control over it. Its lifetime is the same as the lifetime of the VM, though AFAIK this is not documented and could change without warning in the future.

As you've discovered, the Application instance might be destroyed any time your app goes into the background. You will not be notified of this, since the Application has no onSaveInstanceState(...) or other shutdown lifecycle methods like activities do.

It's also possible that the Application will not be destroyed when all your activities have finished. Android may keep the process and VM around, and your Application along with it. If this happens, onCreate() will not be called again the next time your app is launched.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82