Navutils.shoulduprecreatetask Fails On Jellybean
Solution 1:
This is not correct! When you start from the notification you have to create the stack when building the notification as explained here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse
Therefore when creating the notification you'll have to do this:
IntentresultIntent=newIntent(this, ResultActivity.class);
// ResultActivity is the activity you'll land on, of courseTaskStackBuilderstackBuilder= TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack// make sure that in the manifest ResultActivity has parent specified!!!
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stackPendingIntentresultPendingIntent=
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
and then when you click on the UP button you need the regular code, which is:
if (NavUtils.shouldUpRecreateTask(this, intent)) {
// This activity is NOT part of this app's task, so// create a new task when navigating up, with a// synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(intent)
// Navigate up to the closest parent
.startActivities();
} else {
NavUtils.navigateUpTo(this, intent);
}
This works perfectly for me.
Solution 2:
I still don't know why shouldUpRecreateTask fails - looking at the source code for it doesn't help much. But the solution is quite simple - I just add an extra flag value to the Intent that is attached to the notification, and check this in onCreate(). If it is set, then the Activity has been invoked from the notification, so the back stack has to be recreated.
The code looks like this:
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundleb= getIntent().getExtras();
fromNotification = b.getInt("fromNotification") == 1;
setContentView(R.layout.threadlist);
}
@OverridepublicbooleanonHomeButtonPressed() {
if(fromNotification) {
// This activity is not part of the application's task, so create a new task// with a synthesized back stack.TaskStackBuildertsb= TaskStackBuilder.from(this)
.addNextIntent(newIntent(this, COPAme.class));
tsb.startActivities();
}
// Otherwise, This activity is part of the application's task, so simply// navigate up to the hierarchical parent activity.
finish();
returntrue;
}
Solution 3:
I had the same problem as OP. NavUtils.shouldUpRecreateTask always seemed to return false. (JellyBean also) I used the following the achieve the same functionality.
case android.R.id.home:
Intent upIntent = newIntent(this,ParentActivity.class);
upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(upIntent);
finish();
returntrue;
The 'parent' intent could be fetched this way instead of hard coding.
IntentupIntent= NavUtils.getParentActivityIntent(this);
Post a Comment for "Navutils.shoulduprecreatetask Fails On Jellybean"