简体   繁体   中英

losing Activity details after startActivity() method in Android

I have 2 major activity in my project Connect+Details and Menu. After I switch between the first activity to the second everything goes well. the problem start in returning back from 'Menu' to 'Connect+Details'.

when I use the back key to switch between the second activity and go back to the first everything goes well.

problem start when I use this code:

Intent myIntent = new Intent(Menu.this, ConnectActivity.class);
startActivity(myIntent);

I do go back to 'Connect+Details' but every detail in that activity is lost.

my question is simple, how can i go back to previews activity and still have the details used in that activity.

Edit: the same thing goes even when I am in the 4th, 5th activity so finish() didn't help me.

You probably want to either save your data in SharedPreferences or in a database then use the saved data to repopulate your Activity . You can use SharedPreferences to save a key/value pair when you leave your Connect Activity and this will persist even if your app is closed.

If there will be a lot of data stored then you may consider using an SQLite DB . This can be a little trickier to set up and use but may be worth it if you are storing a lot of data...especially if its for different users.

Both of these options will persist your data when you leave the Activity or the program. You could also use a static Array but one of the first two options will probably be better for you.

The Storage Options Docs has some decent examples to get you started, especially if you decide that Shared Preferences will work for you.

Edit

Unless something has changed, you can't store serializable objects in SharedPreferences . But Here is a SO link that discusses saving them to a file and reading from there. Maybe this will help you.

Don't use Intent to move from 'Menu' to 'Connect+Details' instead call finish(); from it

Only use intent in first class to move to second ie;'Connect+Details' to 'Menu'

Don't call finish() in Connect+Details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM