-1

I am using Android Studio / java.

I have some custom variables that I need to save when my application is minimized. I have attempted to make a Bundle, that can be saved though onSaveInstanceState(), which seems to work. The issue is retrieving it once the application is active again. From what I understand, I can get it through onRestoreInstanceState() and Oncreate(). The thing is that onRestoreInstanceState() is never called, and OnCreate() isn't called after being reopened from a minimized state.

I want to be able to press the home button (which currently seem to be saving the bundle correctly), and then reopen the application through the 'Recents' button. When I currently do this, the application resets, and I can't get the Bundle I just saved. How do i do this?

Edit: Calling finish() doesn't work. My activity is on top of another activity, so if I do this, I just end up in the previous activity.

  • Possible duplicate of [Android application retain the state after the app is restored from minimize](http://stackoverflow.com/questions/13683061/android-application-retain-the-state-after-the-app-is-restored-from-minimize) – piotrek1543 Jan 23 '16 at 15:58
  • Doesn't work. My activity is on top of another activity, so if I do this, I just end up in the previous activity. – user3026046 Jan 23 '16 at 16:25

3 Answers3

0

My dear Friend You Have to Use Shared Preference thats it.

(For small tasks which you have asked in question)

Shared Preferences

set values------------->

SharedPreferences pref =   
PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = pref.edit();
edit.putString("username", "billy");
edit.putString("user_id", "65");
edit.commit(); 

get values------------->

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
String username = pref.getString("username", "n/a"); 

There Are Some Others Also (If You Wanna Persist Your Data)

The Android framework offers several options and strategies for persistence:

Shared Preferences - Easily save basic data as key-value pairs in a private persisted dictionary.

Local Files - Save arbitrary files to internal or external device storage.

SQLite Database - Persist data in tables within an application specific database.

ORM - Describe and persist model objects using a higher level query/update syntax.

Each storage option has typical associated use cases as follows:

Shared Preferences - Used for app preferences, keys and session information.

Local Files - Often used for blob data or data file caches (i.e disk image cache)

SQLite Database - Used for complex local data manipulation or for raw speed

ORM - Used to store simple relational data locally to reduce SQL boilerplate

Harsh Sharma
  • 898
  • 1
  • 14
  • 30
0

onSave/RestoreInstanceState is the correct approach if you just need to save state (and not persist it to local storage).

You have to override protected void onSaveInstanceState(Bundle bundle) and use the Bundle from the parameters to add further data. Also, do not forget to call super.onSaveInstanceState(bundle). It’s important to override the protected method, not the public one. Same goes for protected void onRestoreInstanceState(Bundle bundle). You can also restore state in onCreate(), however you need to check for null there.

A tutorial of how to exactly achieve the right behaviour can be found in the official Android training docs.

patloew
  • 1,090
  • 9
  • 13
  • I do not need the data long term. You are describing my current code, which has the problem that protected void onRestoreInstanceState(Bundle bundle) is never called, and onCreate() isn't called when opening from a minimized state. Everything would be fine if only i could get my hands on the Bundle that is saved. – user3026046 Jan 23 '16 at 16:53
  • When `onRestoreInstanceState()` is not called, the Activity was not destroyed in the first place. In this case, the state should be present like it was before leaving the app (you can check the fields in the debugger). Maybe you reset the fields in one of the lifecycle callbacks yourself? – patloew Jan 23 '16 at 16:59
0

onRestoreInstanceState() isn't called unless your app is unloaded from memory (which isn't happening when you're immediately returning to it). Try minimalizing it and then open few other applications and then try to reopen your app - onRestoreInstanceState() should be called.

Note that onRestoreInstanceState() is also always called when you're changing device orientation (usefull for tests).