Issue
I have a main activity (Activity A) which shows one Event (my App is about events managaement), I need to be able to open 2 other subActivities and being able to navigate "up" to their parent (A) again, like in the diagram
open open
Activity (A) ----------> SubActivity (B) ----------> SubActivity (C)
^ ^ | |
| | | |
| | Up | |
| -------------------------- |
| Up |
-----------------------------------------------------------
So, I'm triggering this call from any of the subActivities:
NavUtils.navigateUpFromSameTask(this);
and to make sure not to reload Activity (A), I'm using singleTop
in its AndroidManifest definition (so that when returning to it, it will have the same event with the same state I left it with):
android:launchMode="singleTop"
But the problem is with having a new requirement to open Activity(A) from system notifications with passing an extra value with the eventId
I want to open, it works fine if I removed the singleTop
configuration, but with using it, it gives me the previously loaded instance of the activity without even calling its onCreate
or other similar stuff,
So, what should I do to handle Activity(A)'s sub-navigation along with its notification-navigation?
Solution
By using android:launchMode="singleTop"
the already existing Activity will not be recreated! Also the lifecycle slightly changes: When you have a already existing Activity opened and try to open a "new instance" from your notification Activity.onCreate(Bundle)
is not called again. Instead the method Activity.onNewIntent()
will be called. So you have to override this method in your activity.
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Setup your UI Activity, just like you would to in the onCreate() method
}
Update: to restart the Activity with new Intent:
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// pass this extra from the notification only to make a new instance
boolean makeNewInstance = intent.getBooleanExtra("makeNewInstance", false);
if (makeNewInstance) {
finish(); // Finish the current activity
startActivity(intent); // Start new instance by simply using the intent passed as parameter
}
}
Answered By - sockeqwe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.