Child activity launched from notification does not return to parent navigation

I start the operation - who has parental activity - from the notification in the notification box. The activity of the child is activated. I want the child activity to return to the parent activity when the navigation button up is pressed. This behavior works for a normal application flow. However, when a user enters a child activity through a notification, pressing the up navigation button does not lead the user to parent activity; instead, it terminates the application.

Here is the manifest for the activity that should be triggered when the notification is clicked. It has parent TabbedActivity

 <activity android:name=".activity.AnimalDetailsActivity" android:parentActivityName=".activity.TabbedActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".activity.TabbedActivity"/> </activity> 

Here I create a notice and intent:

 Intent resultIntent = new Intent(context.getApplicationContext(), AnimalDetailsActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context.getApplicationContext()); stackBuilder.addParentStack(AnimalDetailsActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(icon_resource_id) .setLargeIcon(large_icon) .setContentTitle(title) .setContentText(text) .setContentIntent(resultPendingIntent) .setAutoCancel(true); return builder.build(); 

And then send them

 private static void sendNotifications(List<android.app.Notification> notifications) { NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); for (android.app.Notification notification : notifications) { manager.notify(++drawerId, notification); } } 

Inside the target action, I include the up and up arrow navigation:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActionBar().setHomeButtonEnabled(true); getActionBar().setDisplayHomeAsUpEnabled(true); 

I believe that I follow the instructions correctly. So why doesn't the up arrow lead the user to parent activity?

+6
source share
2 answers

According to the documentation, you should implement the onOptionsItemSelected() as follows if you start your activity with a new back stack:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar Up/Home button case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this); if (NavUtils.shouldUpRecreateTask(this, upIntent)) { // This activity is NOT part of this app task, so create a new task // when navigating up, with a synthesized back stack. TaskStackBuilder.create(this) // Add all of this activity parents to the back stack .addNextIntentWithParentStack(upIntent) // Navigate up to the closest parent .startActivities(); } else { // This activity is part of this app task, so simply // navigate up to the logical parent activity. NavUtils.navigateUpTo(this, upIntent); } return true; } return super.onOptionsItemSelected(item); } 
+6
source

To add to the accepted answer, I would isTaskRoot() check:

 if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) { /* ... */ } 

Because when the application starts from the notification, the launched Activity IS is part of the application stack, but we still want the task to be for the parents.

0
source

Source: https://habr.com/ru/post/985559/


All Articles