Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you distinguish onPause() due to onNewIntent vs user action in android

Tags:

android

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?

like image 599
Sani Elfishawy Avatar asked Jun 26 '14 00:06

Sani Elfishawy


People also ask

What is the difference between onPause and onStop mcq?

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.

When onPause method is called in android?

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 .

Is onNewIntent called before onCreate?

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.

How do you handle an onNewIntent fragment?

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.


1 Answers

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();
        }
    }
like image 117
Ash Avatar answered Oct 05 '22 19:10

Ash