How to declare global variables in Android?

前端 未结 17 2932
死守一世寂寞
死守一世寂寞 2020-11-21 04:12

I am creating an application which requires login. I created the main and the login activity.

In the main activity onCreate method I added the following

相关标签:
17条回答
  • 2020-11-21 04:45

    If some variables are stored in sqlite and you must use them in most activities in your app. then Application maybe the best way to achieve it. Query the variables from database when application started and store them in a field. Then you can use these variables in your activities.

    So find the right way, and there is no best way.

    0 讨论(0)
  • 2020-11-21 04:45

    You could create a class that extends Application class and then declare your variable as a field of that class and providing getter method for it.

    public class MyApplication extends Application {
        private String str = "My String";
    
        synchronized public String getMyString {
            return str;
        }
    }
    

    And then to access that variable in your Activity, use this:

    MyApplication application = (MyApplication) getApplication();
    String myVar = application.getMyString();
    
    0 讨论(0)
  • 2020-11-21 04:47

    The suggested by Soonil way of keeping a state for the application is good, however it has one weak point - there are cases when OS kills the entire application process. Here is the documentation on this - Processes and lifecycles.

    Consider a case - your app goes into the background because somebody is calling you (Phone app is in the foreground now). In this case && under some other conditions (check the above link for what they could be) the OS may kill your application process, including the Application subclass instance. As a result the state is lost. When you later return to the application, then the OS will restore its activity stack and Application subclass instance, however the myState field will be null.

    AFAIK, the only way to guarantee state safety is to use any sort of persisting the state, e.g. using a private for the application file or SharedPrefernces (it eventually uses a private for the application file in the internal filesystem).

    0 讨论(0)
  • 2020-11-21 04:50

    On activity result is called before on resume. So move you login check to on resume and your second login can be blocked once the secomd activity has returned a positive result. On resume is called every time so there is not worries of it not being called the first time.

    0 讨论(0)
  • 2020-11-21 04:50

    The approach of subclassing has also been used by the BARACUS framework. From my point of view subclassing Application was intended to work with the lifecycles of Android; this is what any Application Container does. Instead of having globals then, I register beans to this context an let them beeing injected into any class manageable by the context. Every injected bean instance actually is a singleton.

    See this example for details

    Why do manual work if you can have so much more?

    0 讨论(0)
  • 2020-11-21 04:53

    Just you need to define an application name like below which will work:

    <application
      android:name="ApplicationName" android:icon="@drawable/icon">
    </application>
    
    0 讨论(0)
提交回复
热议问题