系统杀死活动时处理Application类

时间:2014-12-01 08:56:24

标签: android

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

public class Applications extends Application {


    // current station
    private Station currentStation;

   ...// Get set method for this Station object ...//

}

在我的第一个活动中,我保存了对象站的新值。

在第二个活动中,我让应用程序中的currentStation在onCreate()中执行某些操作

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_editinfo);
        Applications app = (Applications) getApplication();
        Station current = app.getCurrentStation();

    }

当我在Developer选项中启用“Do not keep activities”来测试活动被杀死的情况时。当我恢复此活动时,“app”返回null。

当活动被杀死时,任何人都有关于应用程序类中处理数据的想法,以避免在这种情况下发生崩溃。

1 个答案:

答案 0 :(得分:0)

"不要保持活动"杀死活动,而不是应用程序。

尝试在Applications app = (Applications) getApplication();再次onResume(),我认为你会得到一个有效的对象。

相关问题