Ares91
Ares91

Reputation: 526

Android Navigate up to parent activity from notification activity

My question is that I have two activities, Activity A and Activity B. A is the main activity and A is the parent activity of activity B. Activity B is accessible by touching a notification or by activity A.

Activity A start activity B like this:

            Intent intent = new Intent(getActivity(), B.class);
            startActivityForResult(intent, RESULT_ACTIVITY_1);

Notification starts activity B like this:

    Intent openIntent = new Intent(context, B.class);
    openIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntentOpen = PendingIntent.getActivity(context, 0 , openIntent, PendingIntent.FLAG_ONE_SHOT );

    contentView.setOnClickPendingIntent(R.id.textView5NotifyOpen,pendingIntentOpen);

Manifest for activity B:

    <activity
        android:name=".B"
        android:parentActivityName=".MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.eee.ccc.MainActivity" />
    </activity>

When activity B close send back to A some data:

        Intent returnIntent = new Intent();
        returnIntent.putExtra("Data",some data);

        setResult(Activity.RESULT_OK,returnIntent);
        finish();

So far everything works well, I'm able to launch activity B from A and from the notification but when I launch it from the notification when B terminate activity A is not called. Now what I want to do is that when I click on a notifications it starts activity B and when B close/finish his parent activity A is launched with setResult(Activity.RESULT_OK,returnIntent); and onActivityResult(int requestCode, int resultCode, Intent data) on activity A is called. THANKS!

Upvotes: 1

Views: 1239

Answers (1)

Bhavin Patel
Bhavin Patel

Reputation: 912

Try TaskStackBuilder

    // Intent for the activity to open when user selects the notification
    Intent detailsIntent = new Intent(this, DetailsActivity.class);

    // Use TaskStackBuilder to build the back stack and get the PendingIntent
    PendingIntent pendingIntent =
            TaskStackBuilder.create(this)
                            // add all of DetailsActivity's parents to the stack,
                            // followed by DetailsActivity itself
                            .addNextIntentWithParentStack(upIntent)
                            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentIntent(pendingIntent);
    ...

Read this android documentation for more details

Upvotes: 0

Related Questions