If you have set up an activity in android to fire onNewIntent() when it receives intents when in the stopped or paused state. It goes through the following sequence if it receives an intent while running:
onPause() -> onNewIntent() -> onResume()
But if you are paused due to user action you just get onPause().
Now if you want to do something in the case the user hid your app but not in the case you are getting a new background intent. Doing something like for example cancelling a recording only when your app gets hidden, it seems you are screwed.
You wont know until sometime in the future whether you got the onPause because a user hid your app or because of an incoming background intent.
Am I missing something or is this completely broken?
Why don't they just send the onNewIntent callback if you get an intent while running. Rather than the pause, onNewIntent, resume sequence?
onPause() −> The system will call this, when an activity going into the background. onStop() − The system will call this, when an activity going into stop.
onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .
OnNewIntent() always get called for singleTop/Task activities except for the first time when activity is created. At that time onCreate is called providing to solution for few queries asked on this thread.
onNewIntent belongs to Activity so you cannot have it in your fragment. What you can do is pass the data to your fragment when it arrives in onNewIntent provided you have the reference to the fragment. Save this answer.
If anyone stumbles upon this again - It would not be possible to detect if onPause() was called due to new intent but onResume() can be detected. Just add BUNDLE_KEY_NEW_INTENT_CALLED when onNewIntent() called and then check the flag in onResume() and take required action.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
intent.putExtra(BUNDLE_KEY_NEW_INTENT_CALLED, true);
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
boolean isResumedDueToNewIntent = (intent != null && intent.getBooleanExtra(BUNDLE_KEY_NEW_INTENT_CALLED, false));
if (isResumedDueToNewIntent) {
actOnNewIntent();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With