Android global variable

前端 未结 14 1022
眼角桃花
眼角桃花 2020-11-21 23:32

How can I create global variable keep remain values around the life cycle of the application regardless which activity running.

相关标签:
14条回答
  • 2020-11-22 00:02

    This global variable works for my project:

    public class Global {
        public static int ivar1, ivar2;
        public static String svar1, svar2;
        public static int[] myarray1 = new int[10];
    }
    
    
    //  How to use other or many activity
    Global.ivar1 = 10;
    
    int i = Global.ivar1;
    
    0 讨论(0)
  • 2020-11-22 00:03

    There are a few different ways you can achieve what you are asking for.

    1.) Extend the application class and instantiate your controller and model objects there.

    public class FavoriteColorsApplication extends Application {
    
        private static FavoriteColorsApplication application;
        private FavoriteColorsService service;
    
        public FavoriteColorsApplication getInstance() {
            return application;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            application = this;
            application.initialize();
        }
    
        private void initialize() {
            service = new FavoriteColorsService();
        }
    
        public FavoriteColorsService getService() {
            return service;
        }
    
    }
    

    Then you can call the your singleton from your custom Application object at any time:

    public class FavoriteColorsActivity extends Activity {
    
    private FavoriteColorsService service = null;
    private ArrayAdapter<String> adapter;
    private List<String> favoriteColors = new ArrayList<String>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite_colors);
    
        service = ((FavoriteColorsApplication) getApplication()).getService();
        favoriteColors = service.findAllColors();
    
        ListView lv = (ListView) findViewById(R.id.favoriteColorsListView);
        adapter = new ArrayAdapter<String>(this, R.layout.favorite_colors_list_item,
                favoriteColors);
        lv.setAdapter(adapter);
    }
    

    2.) You can have your controller just create a singleton instance of itself:

    public class Controller {
        private static final String TAG = "Controller";
        private static sController sController;
        private Dao mDao;
    
        private Controller() {
            mDao = new Dao();    
        }
    
        public static Controller create() {
            if (sController == null) {
                sController = new Controller();
            }
            return sController;
        }
    }
    

    Then you can just call the create method from any Activity or Fragment and it will create a new controller if one doesn't already exist, otherwise it will return the preexisting controller.

    3.) Finally, there is a slick framework created at Square which provides you dependency injection within Android. It is called Dagger. I won't go into how to use it here, but it is very slick if you need that sort of thing.

    I hope I gave enough detail in regards to how you can do what you are hoping for.

    0 讨论(0)
  • 2020-11-22 00:03

    I checked for similar answer, but those given here don't fit my needs. I find something that, from my point of view, is what you're looking for. The only possible black point is a security matter (or maybe not) since I don't know about security.

    I suggest using Interface (no need to use Class with constructor and so...), since you only have to create something like :

    public interface ActivityClass {
    
        public static final String MYSTRING_1 = "STRING";
    
        public static final int MYINT_1 = 1;
    
    }
    

    Then you can access everywhere within your classes by using the following:

    int myInt = ActivityClass.MYINT_1;
    String myString = ActivityClass.MYSTRING_1;
    
    0 讨论(0)
  • 2020-11-22 00:03

    If possible, you should declare the variables that you need to keep alive that haven't been clear by Garbage Collector or Unload by OS in file .so To do it, you must code by C/C++ and compile to .so lib file and load it in your MainActivity.

    0 讨论(0)
  • 2020-11-22 00:04

    You can extend the base android.app.Application class and add member variables like so:

    public class MyApplication extends Application {
    
        private String someVariable;
    
        public String getSomeVariable() {
            return someVariable;
        }
    
        public void setSomeVariable(String someVariable) {
            this.someVariable = someVariable;
        }
    }
    

    In your android manifest you must declare the class implementing android.app.Application (add the android:name=".MyApplication" attribute to the existing application tag):

    <application 
      android:name=".MyApplication" 
      android:icon="@drawable/icon" 
      android:label="@string/app_name">
    

    Then in your activities you can get and set the variable like so:

    // set
    ((MyApplication) this.getApplication()).setSomeVariable("foo");
    
    // get
    String s = ((MyApplication) this.getApplication()).getSomeVariable();
    
    0 讨论(0)
  • 2020-11-22 00:04

    You can use a Singleton Pattern like this:

    package com.ramps;
    
    public class MyProperties {
    private static MyProperties mInstance= null;
    
    public int someValueIWantToKeep;
    
    protected MyProperties(){}
    
    public static synchronized MyProperties getInstance() {
            if(null == mInstance){
                mInstance = new MyProperties();
            }
            return mInstance;
        }
    }
    

    In your application you can access your singleton in this way:

    MyProperties.getInstance().someValueIWantToKeep
    
    0 讨论(0)
提交回复
热议问题