2

我有一个扩展应用程序类的类这是我的应用程序类

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>();

}

我在其中保存了很多数据并在不同的活动和片段中使用它

现在的问题是当我按下主页按钮并在很长一段时间后返回应用程序时它会崩溃。

谁能告诉我如何在离开应用程序时保存这些数据并在我回到应用程序时保留其状态。

请帮忙。

4

1 回答 1

2

您通常不应该将数据放入Application类中。实例的生命周期与Application大多数人的假设不同,您无法控制它。它的生命周期与 VM 的生命周期相同,尽管 AFAIK 没有记录,并且将来可能会在没有警告的情况下更改。

正如您所发现的,只要Application您的应用程序进入后台,该实例就可能被销毁。您不会收到通知,因为Applicationhas noonSaveInstanceState(...)或其他关闭生命周期方法(如活动)。

当您的所有活动完成后,也有可能Application不会被销毁。Android 可能会保留进程和虚拟机,以及您的Application相关信息。如果发生这种情况,下次启动您的应用程序时onCreate()不会再次调用。

于 2015-08-18T19:10:51.050 回答