Save state in Android app

I am developing an Android application in which the first action is to log in. After successful authentication, the user can see the action with 4 tabs. My problem is that when the user clicks the home button, the application should terminate, and when the user starts my application again, he should again see the login screen and after successful login, the user should be able to see the tab, on which it worked before the application terminated (i.e. save state for all tabs). Please help me with this. Thanks in advance.

+3
source share
3 answers

Many applications can provide a way to capture user settings in the settings of a specific application or activity. To support this, Android offers a simple set of APIs.

Preferences are usually pairs of name values. They can be stored as “General Preferences” in various actions in the application (note that at present they cannot be shared between processes). Or it may be something that needs to be preserved for a specific activity.

  • General preferences: General settings can be used by all components (actions, services, etc.) for applications.

  • Processed actions: these settings can only be used in action and cannot be used by other application components.

General settings:

getSharedPreferences Context. (1) (2), .

(1)

public static final String PREF_FILE_NAME = "PrefFile";
   SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE - . , . : MODE_WORLD_READABLE MODE_WORLD_WRITEABLE. MODE_WORLD_READABLE , . MODE_WORLD_WRITEABLE .

(2) - PreferenceManager

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

, , :

 int storedPreference = preferences.getInt("storedInt", 0);

SharedPreference.Editor . Editor - SharedPreference.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

, remove() clear(), .

:

. , . getPreferences() . getPreference getSharedPreferences() .

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

, .

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

, . . Android android.preference. .

, .

+3

Android.

  • : .
  • : Android IO.
  • Sql lite DB:

, .

Android .

+2

Use General Information

SharedPreferences example (from the web world):

     /**
       * get if this is the first run
       *
       * @return returns true, if this is the first run
       */
          public boolean getFirstRun() {
          return mPrefs.getBoolean("firstRun", true);
       }

       /**
       * store the first run
       */

       public void setRunned() {
          SharedPreferences.Editor edit = mPrefs.edit();
          edit.putBoolean("firstRun", false);
          edit.commit();
       }

       SharedPreferences mPrefs;

   /**
   * setting up preferences storage
   */
   public void firstRunPreferences() {
      Context mContext = this.getApplicationContext();
      mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences
   }
0
source

Source: https://habr.com/ru/post/1754962/


All Articles