为什么我的onResume被调用两次? [英] Why is my onResume being called twice?

查看:6155
本文介绍了为什么我的onResume被调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这就是我正在做的事情

Basically, this is what I'm doing

1)设置AlarmManager以执行BroadcastReceiver(BCR)

1) Set AlarmManager to execute BroadcastReceiver (BCR)

Intent intent = new Intent(m_Context, BCR.class);  
intent.putExtras(extras);  
PendingIntent pendingIntent = PendingIntent.getBroadcast(m_Context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, StartTime, pendingIntent)  

2)启动MyActivity BCR

2) Start MyActivity from BCR

@Override  
public void onReceive(Context context, Intent intent) {  
    Intent newIntent = new Intent(context, MyActivity.class);
    newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);  
    context.startActivity(newIntent);  
}

3)如果MyActivity不在,则启用MyActivity

3) Have MyActivity turn on the screen if its not on

@Override  
public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState); 
    getWindow().addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
    getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.myactivity);  
} 

@Overide  
protected void onNewIntent(Intent intent) {  
    super.onNewIntent(intent);  
}  

出于某种原因,我注意到当MyActivity被打开时,它就流了喜欢:

For some reason, I notice that right when MyActivity is opened, it's flow goes like:

onCreate / onNewIntent - > onResume - > onPause - > onResume

onCreate/onNewIntent -> onResume -> onPause -> onResume

我不知道为什么它会立即执行onPause。我注意到这只发生在标志打开被筛选时。有谁知道为什么会这样?有什么方法可以阻止这种行为?

I'm not sure why it does an onPause right away. I notice this only happens when the screened is being turned on by the flags. Does anyone know why this happens? Is there any way this behavior can be prevented?

推荐答案

以防万一其他人遇到这种情况,我似乎注意到这种行为只有当我通过XML布局对活动内的片段进行膨胀时我不知道这种行为是否也发生在Fragments的兼容性库版本中(我正在使用android.app.Fragment)

Just in case anyone else runs into this, I seem to notice this behaviour only when I inflate fragments inside of an activity via XML layout. I don't know if this behaviour also happens with the compatibility library version of Fragments (I'm using android.app.Fragment)

似乎活动将调用活动#onResume 一次调用片段#onResume 到任何添加的片段,然后调用活动#onResume 再次。

It seems the activity will call Activity#onResume once before calling Fragment#onResume to any added Fragments, and then will call Activity#onResume again.


  1. 活动:onCreate

  2. 片段:onAttach

  3. 活动:onAttachFragments

  4. 片段:onCreate

  5. 活动:onStart

  6. 活动:onResume

  7. 片段:onResume

  8. 活动:onResume

  1. Activity:onCreate
  2. Fragment:onAttach
  3. Activity:onAttachFragments
  4. Fragment:onCreate
  5. Activity: onStart
  6. Activity: onResume
  7. Fragment: onResume
  8. Activity: onResume

这篇关于为什么我的onResume被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆