调用覆盖活动而不遵循活动生命周期

时间:2012-05-08 09:11:03

标签: android android-activity

我想从onCreate方法调用onPause方法。这可能吗?

2 个答案:

答案 0 :(得分:1)

没有。您永远不应该对onCreate(或任何其他Activity生命周期方法)进行任何显式调用。系统为您管理Activity生命周期,显式调用这些方法只会干扰。

答案 1 :(得分:0)

我在几个应用程序中有类似的需求。创建一个单独的(可能希望它是一个私有的)方法来完成所有脏工作并从两个地方调用。例如

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    //call your init function here
    init();
    ...
}
@Override
public void onPause() {
    super.onPause(); 
    //call your init function here
    init();
}

//your init stuff
private void init(){
    //do all of the stuff you need to do in onCreate and onPause here
    ...
}