0

I came across some libraries that needs to be in Application class.

Lets have a look at PrefCompat.

First we initialize it

public class Application extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Pref.init(this);
    }
}

and then we can use it anywhere like

Pref.putString("name", "Tushar");

Which is convenient, looks nice and advantage of that is you don't have to pass context again and again inside activities and fragments like

Pref.with(this).putString("name", "Tushar");

it makes the code look beautiful too (in my opinion).

So my question basically is:

Is there any side effects of initializing the library or any class like that?

any thoughts with some explanation will be really appreciated.

Thanks

Atiq
  • 14,435
  • 6
  • 54
  • 69
  • 1
    Everything I listed [here](https://stackoverflow.com/questions/40062979/is-using-the-application-class-as-a-hack-workaround-for-configuration-changes-ac/40063079#40063079) would be a concern. Plus, `onCreate()` of your `Application` is called on the main application thread, so whatever libraries you initialize there need to be cheap. – CommonsWare Oct 15 '16 at 21:29
  • @CommonsWare Thanks, that explains well, one quick question though is there any problem in above case if we use it in background thread? – Atiq Oct 15 '16 at 21:40
  • Sorry, but I am not certain what "if we use it in background thread" means in this context. – CommonsWare Oct 15 '16 at 21:44
  • am sorry I meant to ask after initialization in application (Main thread) if we try to use something like `Pref.putString("name", "Tushar")` in some background thread? you don't have to answer if my question sounds silly :) – Atiq Oct 15 '16 at 21:48
  • Well, you don't really have an opportunity to fork a background thread before `onCreate()` of your `Application` completes, so there is no problem there. You would have to ask the authors of the library what the threading rules are for using the library. – CommonsWare Oct 15 '16 at 21:59

0 Answers0