Search code examples
androidandroid-activityonresumeonpause

activity onPause how to save the interface data


Hello I have in my app 2 activities and I want that when I switch between them the user interface and the Variables wont change is there any way to do it.

Thanks for the help


Solution

  • If you want to save primitive data type (string,int,boolean etc.. ) use SharedPreferences, that will save your values permanently, untill user reinstall (clear data) application. Shared Preferences works like this

    // save string in sharedPreferences
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString("some_key", string); // here string is the value you want to save
                        editor.commit(); 
    

    // restore string in sharedPreferences

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    string = settings.getString("some_key", "");