Android - Keep previously added intent extra when going up to parent activity

android:launchmode
android intent
android-intent flags
android activity lifecycle
android set parent activity programmatically
startactivityforresult
how to move to specific activity on notification click in android
android bring activity to front if exists

Currently I am working with Android and I still have some troubles with understanding the Activity lifecycle.

Let me show you my problem with an example:

My App contains a Navigation Drawer which allows to use to switch between different Fragments. One Fragment (Fragment A) is a list with some items. Clicking one item opens activity B which can be described as a detail view of the item. B receives all necessary information by an Intent, the item model implements Parcelable and it is put as an Extra to the Intent. A button of Activity B opens the map view Activity C.

A -(Item)-> B -> C

Using the Up button in the action bar of C crashes the app when the B Activity onCreate method calls

Item item = bundle.getParcelable("com.example.myapp.model.Item");

Of course, because the detail view B gets all necessary information which has to be displayed from A. How can I solve that problem? I want to be able to store the item somehow when calling C and going up to B again. (Using the back button on C works fine)


If you have implemented up navigation as described in Providing up navigation on the android developers site, you should be able to fix the error simply by changing the launch mode for activity B to "singleTop.". Set this in the application's manifest.xml file, as follows:

<activity ... launchMode="singleTop" ... />

What's happening now, presumably because B's launch mode is standard, is that up navigation is launching a new instance of activity B; it doesn't get the extras that were provided originally by A.

When the launch mode is "singleTop" according to the linked document,

If the parent activity has launch mode singleTop, or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method.

In onNewIntent(), I believe (please check this), that you can just ignore the new intent, because you want to continue using the intent from activity A.

Add an up action, Add an up action. Contents; Declare a Parent Activity; Enable the Up Button. Your app should make it easy for users to find their way back to the� Whenever you need data from an activity to be in another activity, you can pass data between then while starting the activities. Intents in android offer this convenient way to pass data between activities using Extras. Creating multiple activities to display contents of same properties is not an ideal solution.…


** Do not assume activity B still exists when you press up from B **

You mentioned the crash occurs because 'normally'

the detail view B gets all necessary information which has to be displayed from A.

So in this flow activity C will have to provide that information to B. You can do this using the getParentActivityIntent method. The base implementation of that method looks at the parentActivityName attribute in your manifest to create an intent. If you override it (in activity C) you can add the extras you need.

@Nullable
@Override
public Intent getParentActivityIntent() {
    Intent intent = super.getParentActivityIntent();
    if (intent != null)
        enhanceParentActivityIntent(intent);
    return intent;
}

private void enhanceParentActivityIntent(@NonNull Intent intent) {
    intent.putExtra("...", "...");
}

Understand Tasks and Back Stack, For example, an email app might have one activity to show a list of new… Save data in a local database This new activity is added to the back stack. At this point, the user can also switch back to Task B by going Home and in the < activity> manifest element and with flags in the intent that you pass to� In Android an Activity can be started for some results, means an Activity can return some result to the Parent/Previous Activity. An Activity can return String, Int, float, boolean etc as Result. In this post I have two Activities MainActivity and SecondActivity. Second Activty returns a String to the First MainActivity as Result.


I would use an application class to store the global data like so: Using the Android Application class to persist data This serves as a singleton object which can be referred to by all activities

Introduction to Activities, An Activity is an application component that provides a screen with Each activity is given a window in which to draw its user interface… which app to use in performing a task, that's an intent filter at work. EXTRA_TEXT, textMessage) If you declare a <uses-permission> element for a parent activity,� Open it in Eclipse and switch to the XML editor tab. Find the section containing your app's main Activity - you should see an Intent Filter element there already, indicating MAIN action and LAUNCHER category. After the existing Intent Filter element, inside the main Activity element, add another Intent Filter: <intent-filter> </intent-filter>


Start an Activity from a Notification, Preview videos � Recommendations in Android N and earlier When you start an activity from a notification, you must preserve the user's As long as you've defined the parent activity for each activity as If necessary, you can add arguments to Intent objects in the stack by calling TaskStackBuilder. To start an activity in an Android app, you don’t call a method. Instead, you fire up an intent. So far, so good. But what feature of an intent takes the place of a method call’s return value? In the following code, an activity asks for a result. package com.allmycode.results; import android.app.Activity; import android.content.Intent; import …


Activity, At this point your activity is at the top of its activity stack, with user input going to it. The activity is still visible to user, so it's recommended to keep it visually In addition, it can optionally return back an Intent containing any additional data it wants. Applications that wish to supply extra Intent parameters to the parent stack� If you want your activity to receive implicit intents, it must include a category for "android.intent.category.DEFAULT" in its intent filters, as shown in the previous <intent-filter> example. Data test. To specify accepted intent data, an intent filter can declare zero or more <data> elements, as shown in the following example:


Activity | Desarrolladores de Android, At this point your activity is at the top of its activity stack, with user input going to it. The activity is still visible to user, so it's recommended to keep it visually In addition, it can optionally return back an Intent containing any additional data it wants. Applications that wish to supply extra Intent parameters to the parent stack� The above code snippet tells Android that the parent activity of DetailActivity is MainActivity. Now that we’ve done this, we can go back to our DetailActivity and configure the navigation. To do this however, we’ll need an extra constant to do the same task as EXTRA_COUNTRY did in MainActivity.