0

我在 Play 商店中启动并运行了一个应用程序,我遇到了 android OS 杀死我的应用程序的问题。

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!!!!.

我注意到的是,它不是从启动画面开始的应用程序,而是从它离开的地方开始,因为我的所有数据都丢失了,所以它给了我 NULL POINTER EXCEPTIONS。

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

如何检查我的应用程序是否被杀死并从启动屏幕加载?

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

4

1 回答 1

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

}
于 2015-12-02T16:44:26.567 回答