android生命周期问题

CoderLeon 发布于 2015/07/01 16:28
阅读 125
收藏 0

有2个活动,第一个MainActivity,第二个DisplayMessageActivity。

有如下操作:

1.Main活动按钮启动Display活动,生命周期如下,没问题。


  1. 11-15 02:56:16.904: D/MainActivity(1652): On Pause
  2. 11-15 02:56:16.932: D/DisplayMessageActivity(1652): On Create
  3. 11-15 02:56:17.004: D/DisplayMessageActivity(1652): On Start
  4. 11-15 02:56:17.004: D/DisplayMessageActivity(1652): On Resume
  5. 11-15 02:56:17.656: D/MainActivity(1652): On Stop

2.在Display活动按下顶部actionBar的导航回退,生命周期如下:

  1. 11-15 02:57:27.788: D/DisplayMessageActivity(1652): On Pause
  2. 11-15 02:57:27.808: D/MainActivity(1652): On Destory
  3. 11-15 02:57:27.880: D/MainActivity(1652): On Create
  4. 11-15 02:57:27.880: D/MainActivity(1652): On Start
  5. 11-15 02:57:27.880: D/MainActivity(1652): On Resume
  6. 11-15 02:57:31.104: D/DisplayMessageActivity(1652): On Stop
  7. 11-15 02:57:31.104: D/DisplayMessageActivity(1652): On Destory
3.接着重复第一个操作,在Display中按下模拟器的回退按钮(back),生命周期如下:
  1. 11-15 02:58:50.908: D/DisplayMessageActivity(1652): On Pause
  2. 11-15 02:58:50.912: D/MainActivity(1652): onActivityResult First Param:123 //传参
  3. 11-15 02:58:50.912: D/MainActivity(1652): onActivityResult Secenod Param:456 //传参 
  4. 11-15 02:58:50.912: D/MainActivity(1652): On Restart
  5. 11-15 02:58:50.912: D/MainActivity(1652): On Start
  6. 11-15 02:58:50.912: D/MainActivity(1652): On Resume
  7. 11-15 02:58:52.300: D/DisplayMessageActivity(1652): On Stop
  8. 11-15 02:58:52.300: D/DisplayMessageActivity(1652): On Destory

在Main onActivityResult回调方法打印出requestCode和resultCode

问题,为什么2和3操作的周期不一样,并且在2操作中销毁重现创建活动,取不到返回数据。而3操作中却不一样?

求解答谢谢



加载中
1
小红书
小红书

还真没注意过这个问题,脑补了一下

http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly

The system always creates a new instance of the activity in the target task and routes the intent to it.

action bar 上的back不是简单的back

解决方法就是 设置 MainActivity的启动模式 singleTop

CoderLeon
CoderLeon
感谢,我搜了半天没找到原因
0
CoderLeon
CoderLeon
谁来回答下呢
0
notreami
notreami

RESULT_OK 是否设置的问题。。。

返回顶部
顶部