Onnewintent() Lifecycle and Registered Listeners

onNewIntent() lifecycle and registered listeners

onNewIntent() is meant as entry point for singleTop activities which already run somewhere else in the stack and therefore can't call onCreate(). From activities lifecycle point of view it's therefore needed to call onPause() before onNewIntent(). I suggest you to rewrite your activity to not use these listeners inside of onNewIntent(). For example most of the time my onNewIntent() methods simply looks like this:

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// getIntent() should always return the most recent
setIntent(intent);
}

With all setup logic happening in onResume() by utilizing getIntent().

Which lifecycle event is best to register/unregister listeners?

An alternative approach may be to postpone the processing currently done in onActivityResult() until after the listeners are registered in onResume().

Possible ways of doing this include posting to the message queue, e.g. using a Handler, setting a Runnable object to be called by onResume, or simply storing the result data received by onActivityResult().

This would also ensure that the activity really has come to the foreground when the listener methods are called.

Calling onNewIntent() from a notification

Try setting launchMode of your activity as "singleTop" in manifest.

From Official documentation.

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Listen to Intent changes

You are looking for onNewIntent of Activity, that will trigger each time the Activity receives a new Intent.

This works exactly the same as for standard Android.

Return to MainActivity with added data from another Activity

Ok, I've found a solution (also thanks to shimi_tab's answer and Budius comment about onNewIntent):

In my Activity C when I want to return:

Intent home = new Intent(this, A.class);
home.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
home.putExtra("myKey", true);
startActivity(home);

In my Activity A:

// This allows us to use getIntent to get the latest intent, instead of the first Intent used in the onCreate
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}

@Override
protected void onResume(){
super.onResume();

if(getIntent().getExtras() != null && getIntent().getExtras().getBoolean("myKey")){
// Do something else after returning from C
}
else{
// Do regular things on a normal onResume (like back from B or Settings)
}
}

NOTE (trivial to the original question): In my case I use Google Services in my Activity A, where I had googleApiClient.onConnect(); in the onStart() method. So, I also had to add the following to the onConnected() method:

@Override
public void onConnected(Bundle b){
Bundle extras = getIntent().getExtras();
if(extras == null || (extras != null && !extras.getBoolean("myKey"))){
// Do regular stuff
}
else{
// Do something else after we returned from C
}
}


Related Topics



Leave a reply



Submit