1

这与以下问题有关:

检测到返回键但活动未结束

基本上,我必须按两次 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

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

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

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

它是否与被定义为 android:launchMode="singleTask" 的应用程序有关?

关于如何定义/启动 Activity 启动 C 是否有任何要求?Activity 启动 Activity 启动 C 怎么样?

4

0 回答 0