简体   繁体   中英

Does static class have the same lifecycle with Android Application?

At the very first, my code looks like this. The datacenter class need context to reach SharedPreference.

class App extends Application
{
    private DataCenter dataCenter;
    @Override
    public void onCreate() {
        super.onCreate();
        dataCenter = new DataCenter(this);
    }
}

When I use dataCenter in my activity, I had to getApplicationContext(), and cast to my App, then get the dataCenter object.

And then I searched stackoverflow, and realize I can make App has a static field to reference itself.

class App extends Application
{
    private DataCenter dataCenter;
    public static App me;
    @Override
    public void onCreate() {
        super.onCreate();
        App.me = this;
        dataCenter = new DataCenter(this);
    }
}

By doing so, I no longer need to call getApplicationContext() and make the casting, just need App.me.getDataCenter(), in my activity class. And also in the App's constuctor, I could even, not to pass context to dataCenter's consctrutor, just ref App.me in DataCenter class directly.

(Q1) I am woundering if what I am doing is OK, any risk here?

After that, I realise if I want to use dataCenter in my activity, "App.me.getDataCenter()" is still too long for me

(Q2) Can I just make the DataCenter class static as it can access App.me to get the context statically?

(Q3) is static class has the same lifecycle with android's Application ? and the Application class always initial first?

First of all, it's better to change scope of the variable me to private, change its name to something better and more descriptive (eg instance ) and create get() method, which will return instance of the Application. When you leave this variable public, it can be changed and I guess you don't want it.

(Q1) I am woundering if what I am doing is OK, any risk here?

After that, I realise if I want to use dataCenter in my activity, "App.me.getDataCenter()" is still too long for me

I think, you should use one instance of SharedPreferences class. If DataCenter uses SharedPreferences , you shouldn't mix context of application and activity. You should choose one context and keep using it constantly. In my opinion, context of the application is a better choice, because you may want to use this object in many activities. Moreover, you don't need to call App.me.getDataCenter() everytime in the activity. Just pass it inside the Application class.

(Q2) Can I just make the DataCenter class static as it can access App.me to get the context statically?

You can, but you should avoid static classes. When you use static classes, it's harder to write unit tests for your application. Moreover, static objects are not elected for garbage collection while the class is loaded. It means, that your application will consume more memory of the device and will be less efficient.

(Q3) is static class has the same lifecycle with android's Application ? and the Application class always initialed first?

Application's Lifecycle shouldn't change while using static variables in class which derives from Application class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM