0

I want to create a class for some value such that changes to it is reflected across all the classes.

For example:

If I create a class

public class CheckForAlreadyLoggedIn extends Application {

    public int getState(){ return state; }
    public void Setstate(int s) { state = s; }

    private static int state;
}

I want to set and get the value in other different classes in android studio. I am thinking is there a possibility to achieve this situation in android studio, with the help of creating the global object for that class and use it everywhere freely?

help me to resolve this issue....

thanks in advance..........

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • refer this https://stackoverflow.com/questions/47766894/calling-a-method-from-another-class-is-causing-app-to-crash/47772559#47772559 - may be this can help in your situation. – Bhoomika Patel Apr 16 '18 at 04:13

2 Answers2

0

Instead of that why not try to use SharedPreference?

please check this example of how to read, write and update values inSharedPreference

Moustafa EL-Saghier
  • 1,721
  • 1
  • 13
  • 43
0

There are different approaches available to achieve this.

  1. If your intention is to have just in memory representation of data such that the data doesn't persist across application restart, then you implement the class as Singleton
  2. If your intention is to allow the data to be persisted across application restart, then you can implement SharedPreference
Sagar
  • 23,903
  • 4
  • 62
  • 62