生命周期乱序,与'检测到后退键但活动未结束有关

时间:2012-04-29 15:14:49

标签: android

这与以下问题有关:

back key detected but Activity not ending

基本上,我必须按两次BACK才能退出我的Activity(我称之为C)有一个名为sessionActive()的状态方法。

如果sessionActive()是FLASE并按下BACK,则活动C应该结束。

我在onKeyDown()/ onBackPressed()中包含了对finish()的调用

我已经通过onCreate(),onResume()等方法进行了跟踪,并且不理解我看到的序列。来自logcat的以下代码段显示生命周期方法似乎无序发生:

C.onCreate() 
C.onStart() 
C.onResume() 
C.onKeyDown() sessionActive()==false, keyCode = BACK              
                      This is the 1st BACK press that should get us out but doesn't

C.finish() sessionActive is FALSE                                
                      Shouldn't this execute onStop()?

C.onPause() sessionActive is FALSE      <- Why are these running?
C.onRestart() sessionActive is FALSE       
C.onStart() sessionActive is FALSE      
C.onResume() sessionActive is FALSE

C.onStop() sessionActive is FALSE       <- These makes sense.
C.onDestroy() sessionActive is FALSE

C.onPause() sessionActive is FALSE      <- Why didn't the previous onDestroy() stop us?

C.onResume() sessionActive is FALSE     <- Confusing!!!

C.onKeyDown() sessionActive is FALSE, keyCode = BACK
                    This is 2nd BACK press that causes Activity C to exit.

C.finish() sessionActive is FALSE
C.onPause() sessionActive is FALSE      <- Why is there is no onRestart here?

C.onStop() sessionActive is FALSE       <- This is the sequence I expect.
C.onDestroy() sessionActive is FALSE

这是正常的吗?如果是,我不明白为什么生命周期的执行方式不同。

是否与活动C的启动方式有关:

    Intent startActivity = new Intent();
    startActivity.setClass(this,C.class);
    startActivity.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(startActivity);

它可以与定义为android的应用程序相关:launchMode =“singleTask”??

是否有关于如何定义/启动Activity启动C的要求?如何启动活动启动C?

0 个答案:

没有答案