Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my onResume being called twice?

Basically, this is what I'm doing

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) 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) 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);  
}  

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

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

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?

like image 294
Huy T Avatar asked Apr 16 '13 00:04

Huy T


3 Answers

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)

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. Activity:onCreate
  2. Fragment:onAttach
  3. Activity:onAttachFragments
  4. Fragment:onCreate
  5. Activity: onStart
  6. Activity: onResume
  7. Fragment: onResume
  8. Activity: onResume
like image 145
ainesophaur Avatar answered Nov 20 '22 12:11

ainesophaur


if you trying request permissions every time it can cause such problems, just check if you already granted them

requestPermissions can cause it:

onCreate
onStart
onResume
onPause
onResume

Use this method ContextCompat.checkSelfPermission(context, permission) to check if permission was granted or not before requesting it

This method returns int and you can check it with PackageManager.PERMISSION_GRANTED constant

like image 34
user924 Avatar answered Nov 20 '22 12:11

user924


If you have ES File Explorer then FORCE STOP it. Somehow, they interrupt your app's lifecycle (comments suggest some kind of overlay).

My issue with onResume being caused twice was because onPause was somehow being called after the activity was created.. something was interrupting my app.

And this only happens after being opened for the first time after installation or built from studio.

I got the clue from another post and found out it was because of ES File Explorer. Why does onResume() seem to be called twice?

As soon as I force stop ES File Explorer, this hiccup behavior no longer happens... it's frustrating to know after trying many other proposed solutions. So beware of any other interrupting apps like this one.

like image 18
TWL Avatar answered Nov 20 '22 13:11

TWL