Skip to content Skip to sidebar Skip to footer

Entering App Exactly As It Was Left

I know there must be douzens of answers to this question out there, but either i cant't find them or I don't understand them. The Question: How do I get my app to exactly start as

Solution 1:

There is no "out of the box" way of doing it. You could save the current state of your Activity in some way (More on persistence)

Then you need to be able to rebuild the desired state of the persisted state in your Activity lifecycle

You could save and load with the shared preferences for example:

publicvoidsaveState(YourState state) {
    SharedPreferences sharedPreferences = app.getSharedPreferences(R.string.preference_file_key, Context.MODE_PRIVATE)
    sharedPreferences.edit()
        .putString("CustomAtt", state.getCustomAtt())
}

public YourState loadState() {
    SharedPreferences sharedPreferences = app.getSharedPreferences(R.string.preference_file_key, Context.MODE_PRIVATE)
    String customAtt = sharedPreferences.getString("CustomAtt", "DefaultValue")
    returnnew YoutState(customAtt)
}

And use it like this

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    YourStatestate= loadState();
    // Rebuild your activity based on state
    someView.setText(state.getCustomAtt())
}

Solution 2:

You can store such values in SharedPreferences.

https://developer.android.com/training/basics/data-storage/shared-preferences.html

It is using key-value approach for saving. So you can save some values and read it from SharedPreferences whenever you want to.

This is the best approach for small data, that can be used on the app launch. So you can quit your app and the data is still present - so can be read on the next app launch.

Solution 3:

Or save the condition of your program to a text file, so that the program can "translate" it back into conditions before it stops, or what I do not recommend, it saves every object created with ObjectOutputStream.

Post a Comment for "Entering App Exactly As It Was Left"