如果操作系统杀死应用程序,则处理应用程序活动

时间:2015-12-02 16:27:37

标签: android android-activity android-applicationinfo

我有一个应用程序并在Play商店中运行,我有一个问题,Android操作系统会杀死我的应用程序。

Scenario/Steps :

1) Open app, move to any screen.
2) Minimise the app by clicking HOME button.
3) Open several other apps.(say 14 to 15 apps).
4) And now launch my app it CRASHES!!!!.

我注意到的是,不是应用程序从启动画面开始,它从它离开的地方开始,因为我的所有数据都丢失了,它给了我一点点指示。

理想情况下,我的应用程序应该从启动画面开始,因为我在那里加载了所有数据并将其传递给其他活动。

如何检查我的应用是否已被杀死并从启动画面加载?

我也扩展了Application类,但我不知道如何使用它。

1 个答案:

答案 0 :(得分:0)

我认为你可能正在使用静态变量,只是一种预感。无论如何,你可以在mainActivity onCreate或onResume:

中尝试这个
if (isTaskRoot()) {
    // This activity is at root of task, so launch main splash screen 
} else { 
    // This activity isn't at root of task, so continue
} 

但是我认为这样可以摆脱真正的问题:相反,我会在Activity类中查看onSaveInstanceState:

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current  state 
    savedInstanceState.putString("myStaticVariable", sStaticVariableAreBad);
    // Always call the superclass so it can save the view hierarchy state 
    super.onSaveInstanceState(savedInstanceState);
} 

然后在onCreate上检查活动是否先前被销毁:

// Check whether we're recreating a previously destroyed instance 
if (savedInstanceState != null) {
    // Restore value of members from saved state 
    sStaticVariableAreBad = savedInstanceState.getString("myStaticVariable");

}