1

I figured out a little bit about Android's life cycle.

So even static variables can be lost if OS faces with a shortage of resource, right? (Android static variables are lost)

E.g., the static final String variable like

public static final String OPTION = "option";

can be lost when the app is paused.

Then how can I declare constants which can be accessed all the time through the app's life cycle besides using 'strings.xml' because I cannot statically access the file.

Is it possible to declare constant variables whose value is always kept?

ghchoi
  • 4,812
  • 4
  • 30
  • 53
  • In Java, a better alternative to using static String constants is using enums. They provide better features than using static Strings. – Hitesh Pamnani Nov 27 '17 at 05:11
  • Possible duplicate of [this](https://stackoverflow.com/questions/11150701/which-is-best-way-to-define-constants-in-android-either-static-class-interface) – Hemant Parmar Nov 27 '17 at 05:12

3 Answers3

3

You are correct that the Android OS can kill your app's process at any time, and this can cause static variables to be "lost". However, for simple constants, this is not an issue. As soon as your app starts back up again, those constants will be re-initialized and be available for use. So things like

public static final String MY_APP_NAME = "Stack Overflow";
public static final int VERSION_NUMBER = 1;

are totally safe to use. You only run into trouble with the OS killing your process when you have static variables like this:

public static MyObject;

whose value can change over the lifetime of the app's process.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
1

You can have getters in your Application class with a condition that if the variable is null get it from shared preference else return it from memory.

The setters of such variable will set the value to shared preference as well.

public class MyApplication extends Application {

  private String var;

  public String getVar() {
     if(var == null){
         return getVarFromSP();
     }
     return var;
  }

  public void setVar(String s) {
       var = s;
       storeVarToSP(s);
  }
}
Jainendra
  • 24,713
  • 30
  • 122
  • 169
0

You can create a singleton class that extends Application and put the global variables there. The variables will be accessible from anywhere unless the app is closed.

E.g.

public class MyApplication extends Application {

public static final String OPTION = "option";

}

Later add the class in manifest,

<application
      android:name="MyApplication"
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      ............
  </application>

Finally to get the variable from any activity,

String option = ((MyApplication) this.getApplication()).OPTION;

or

String option = MyApplication.OPTION;

In your case, the variable was lost because after a rotation or config change the app lifecycle starts over and resets all predefined variable.

For more info check: Android global variable

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50
  • Can't we just use `String option = MyApplication.OPTION;` instead of `String option = ((MyApplication) this.getApplication()).OPTION;` – Sanjeet Nov 27 '17 at 05:31