Redirect to activity with saving / creating action stack

I want to get started with a notification. I want to open an event that is the successor to some other activities.

Example actions: IntroActivityPhotosSpecificPhoto. What I want to achieve: if the user clicks on the notification, I want to open SpecificPhotoactivity. Keep in mind that the application may work (for example, it is displayed PhotosActivity), or it can be turned off.

  • I want to keep the functionality of the back button (go PhotosActivityback).
  • In the notification, click, I need to run IntroActivity, because the user must log in here if he is not.

I tried following (using constants in actions, code):

On PhotosActivity onCreate: redirectToActivity();

RedirectToActivity Method:

private void redirectToActivity() {
    Intent intent = getIntent();
    int activityCode = intent.getIntExtra("code", 0);

    switch (activityCode) {
        case SpecificPhotoActivity.CODE:
            startActivity(new Intent(this, SpecificPhotoActivity.class));
            break;
        default:
            return;
    }
}

Using this approach, I can go through the entire stack of activity and move on to the activity that I want. However, this approach does not work in every case. Sometimes it’s activity_codenot set (I don’t know why), and therefore we finish the first action.

Is there a more professional approach to solve this problem? I believe that this needs to be somehow solved in many applications.

+4
source share
1 answer

What you want is called TaskStackBuilder.

Here you must create an intent that will move to SpecificPhotoActivity:


    Intent action = new Intent(context, SpecificPhotoActivity.class);

    PendingIntent pendingIntent = TaskStackBuilder.create(context)
            .addNextIntentWithParentStack(action)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

To correctly specify the action stack, you must provide android:parentActivityNameinside the manifest file:

<application ...>
    <activity android:name=".SpecificPhotoActivity"
        android:parentActivityName=".PhotosActivity"/>
</application>

, SpecificPhotoActivity PhotoActivity, TaskStackBuilder , , "Back" SpecificPhotoActivity.

:


    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle(...)
            .setContentText(...)
            .setSmallIcon(...)
            .setContentIntent(pendingIntent)
            .build();

    manager.notify(NOTIFICATION_ID, notification);

SpecificPhotoActivity. "", PhotosActivity.

, . , , . , :


    PendingIntent pendingIntent = null;
    if (authorized) {
        Intent action = new Intent(context, SpecificPhotoActivity.class);

        pendingIntent = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(action)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    } else {
        Intent action = new Intent(context, IntroActivity.class);
        action.putExtra("photos_flow", true);
        pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
    }

, IntroActivity :


    void onAuthorized() {
        if(getIntent().getBooleanExtra("photos_flow", false)) {
            // most possibly you should pass some id into SpecificPhotoActivity intent
            Intent[] intents = new Intent[]{new Intent(this, PhotosActivity.class), new Intent(this, SpecificPhotoActivity.class)};
            startActivities(intents);
            finish();
        }
    }

+2

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


All Articles