Skip to content Skip to sidebar Skip to footer

Correct Way To Get The Instance Of Application In Android

Which of these ways is more proper for getting the instance of Application Initialise static field in Application.onCreate() and provide static access to it public class MyApplica

Solution 1:

I would recommend method 3 if you only need the instance of the Application.

I would recommend method 1 if you had additional methods in your Application class because you can more clearly do

MyApplication.getInstance().foo();

Method 2 is just a shortcut for method 3, so I wouldn't recommend it.


All in all, it's a matter of preference. There is no one "correct" way because they'll all work.

Solution 2:

(getActivity().getApplication() as MyApplication)

This will return the instance of MyApplication

Solution 3:

(getActivity().getApplication() instanceOf MyApplication) should be the correct way

Post a Comment for "Correct Way To Get The Instance Of Application In Android"