Katherine99
Katherine99

Reputation: 990

Entered Values in next Activity and Back

I have an app which consists of 7 pages and the user can go through these 7 pages with a button. He can go back to the previous page with the own button of the mobile device. As said, if I click the button the user goes to the next page. Besides, if the user clicks the "Back" button of the device it goes back to the previous activity and all the entered values remain the same. However, what I want is that if the user goes to the next activity and the user had entered before some values in that activity, these values remain there.(not empty as is happens now). I guess that this happens because everytime I click the button, a NEW activity is launched.

Is there a method to achieve this without using SharedPreferences, XML, etc...?

Upvotes: 0

Views: 117

Answers (3)

Rahul Gupta
Rahul Gupta

Reputation: 5295

Before starting another activity, you can put key value pairs like hashmaps in intents and you can retrieve these values in the activity you are calling. You can look into :-

Intent returnIntent = new Intent(getApplicationContext, SecondActivity.class);
returnIntent.putExtra("name","abc");
startActivity(returnIntent,11);

And in you other activity result method, you can do

if(requestCode == 11){
Bundle bundle = data.getExtras();
String name = bundle.getString("name");
}

You can look into this.

Upvotes: 1

Ali Imran
Ali Imran

Reputation: 9217

When the back is pressed then by default the activity is destroyed. the only way to save its data is to store it somewhere just like SharedPreferences to avoid this activity destruction you can set it as single instance in menifest then on back press call this method moveTaskToBack(true); Now whenever you launch the activity again it will not recreate itself . also remove the super.onBackPressed(); method from that method. (onBackPressed())

in manifest do like this

<activity android:name=".YourActivity" android:launchMode="singleInstance" />

Upvotes: 3

anand
anand

Reputation: 416

you can achieve this by calling startActivityForResult() for starting the next Activity...when you come back to previous activity your data will remain on the screen

Upvotes: 0

Related Questions