x-ray
x-ray

Reputation: 3329

Change intent extras for back navigation intent

I have an app with the following flow (all activities in the same app):

Now I have two scenarios for pressing the back button in activity C:

I tried removing the flag from the Intent in activity B's onCreate, but it doesn't work:

final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras.containsKey("autoContinue")) {
    intent.removeExtra("autoContinue");
    continue();
}

How can I remove the "autoContinue"=true flag from the intent extras when navigating back from activity C?

Upvotes: 1

Views: 2296

Answers (2)

Shajeel Afzal
Shajeel Afzal

Reputation: 5953

You can pass extras to previous activity while finishing if you have started the activity with startActivityForResult method. See the following code example:

public void finishActivity() {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("KEY", "Value");
        getActivity().setResult(Activity.RESULT_OK, returnIntent);
        getActivity().finish();    
}

Also, I want to recommend you not hard code the keys. Read the blog post that I have written on this topic.

Upvotes: -1

Vesko
Vesko

Reputation: 3760

You're on the right track in removing the extra from the intent. However the Intent associated with the Activity still has the extra in it. You need to call setIntent() after removing the extra. So your code will look like this:

final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras.containsKey("autoContinue")) {
    intent.removeExtra("autoContinue");
    setIntent(intent);
    continue();
}

Upvotes: 4

Related Questions