Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to come back to my parent-activity without losing data?

I have an activity A with a "child activity" which is the activity B. When I create an activity, like in this case in android, I choose for activity B, that his parent shall be activity A.

So, when I start my application, I have an icon in the appbar in the activity B, where I can go to the activity A.

In my case, the user for example edit-text and manipulate some variables in the activity A and call activity B with Intent.

NOW, in activity B, when I click on the "back" icon, I will go back to the parent activity in this case activity A and all manipulated data are "away", because the screen will be relaunched.

But when I dont click on the icon in the activity B but instead click "back" on my mobile phone, I will come back to activity A and all manipulated data will be still there.

SO my question is, is there a way, to come back to activity A by clicking the back icon in activity B, without that the screen will be relaunched. Because this icon was automatically added, when I say, which activity have to be the parent class. I would like, that I can go back with this icon, but not relaunch the screen, instead continue, where I was "last" time.

I show you a part of my manifest file for the activity B:

<activity
            android:name=".strassenfuehrer_screen.VideoPlayerHandlungsleitfadenStrassenfuehrerActivity"
            android:label="Straßenführer  >  Handlungsleitfaden  >  VideoPlayer"
            android:parentActivityName=".strassenfuehrer_screen.MitHandlungsleitfadenStrassenfuehrerScreenActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="de.derdoenerdon.ressourcencockpit.strassenfuehrer_screen.MitHandlungsleitfadenStrassenfuehrerScreenActivity" />
        </activity>

Here you can see the "back icon" in the activity B.

enter image description here

For help, I would be very thankfull, I am new in android and have difficulties with it.

Thanks a lot

like image 956
Josi Allen Avatar asked Apr 28 '18 12:04

Josi Allen


People also ask

How do I get back the result from child activity to parent in android?

In Child Activity: Intent data = new Intent(); data. putExtra("myData1", "Data 1 value"); data. putExtra("myData2", "Data 2 value"); // Activity finished ok, return the data setResult(RESULT_OK, data); finish();

How will you get the data in second activity?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do I set parent activity?

Declare a Parent Activity You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.

How pass data from second activity to first activity when pressed back Android?

Start Activity2 with startActivityForResult and use setResult method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult for updating TextView with EditText data from Activity2. If you can, also use SharedPreferences for sharing data between Activities.


2 Answers

In your manifest for Activity A add this attribute:

android:launchMode="singleTask"

This should solve your problem.

This makes sure that there is only one instance of Activity A in the BackStack. Therefore Activity A is only created once per Task.

Hope this helps.

for more information https://developer.android.com/guide/components/activities/tasks-and-back-stack

like image 154
will Avatar answered Sep 20 '22 19:09

will


You can override onSaveInstanceState() into your Activity and save your data before leaving that Activity.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    savedInstanceState.putInt("KEY1", Value1);
    savedInstanceState.putInt("KEY2", Value2);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

On coming back to that Activity you can get the saved data as bundle into you onCreate() method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        int val1 = savedInstanceState.getInt("Key1");
        int val2 = savedInstanceState.getInt("Key2");
    } else {
        // This is the case when you are openning this Activity for the for the first time
    }
}
like image 21
Ghulam Moinul Quadir Avatar answered Sep 19 '22 19:09

Ghulam Moinul Quadir