Why Extend the Android Application Class

Why extend the Android Application class?

Offhand, I can't think of a real scenario in which extending Application is either preferable to another approach or necessary to accomplish something. If you have an expensive, frequently used object you can initialize it in an IntentService when you detect that the object isn't currently present. Application itself runs on the UI thread, while IntentService runs on its own thread.

I prefer to pass data from Activity to Activity with explicit Intents, or use SharedPreferences. There are also ways to pass data from a Fragment to its parent Activity using interfaces.

Why Do We Extend Application Class In Android


Android apps run properly even without extending it.

Android apps can run properly without extending it. They can run properly without extending Activity. You extend Application when it does something useful for you.

Can anyone explain this scenario as why exactly do we extend it.

It is used for per-process initialization that should always happen, not just for certain activities or other components.

So, for example, you might initialize:

  • Dependency injection (e.g., Dagger)
  • Crash logging (e.g., ACRA)
  • Diagnostic tools (e.g., LeakCanary, Stetho, Sonar)
  • Global pools (e.g., OkHttpClient)

Ideally, you initialize as little as possible, simply because this initialization is performed on the main application thread every time your process is forked. But, it is often convenient for things that should be set up before any of your application logic is executed.

Why my application class which extends android.app.application is not working?

You should also declare your custom Application class in manifest:

<application
android:name=".<your packages to the class>.MyApplication"
...>

Android: extending Application class. Why do we need to implement singleton pattern?

An Application class is an access point to application context and generally it is used as a initializator for all application-scoped dependencies like your database object. This class is initialized only once per application and persists in memory until application is no longer in memory. So it is a natively created singleton.

By having such static access point to application you may have access to the application context in any class and in general case this context is much better for retrieving resources, system services etc. when you need such dependencies in your custom classes, because it doesn't hold a link to the activity and view so is a leak-safe. Of course in your example the Activity has an access to application, but you have to cast context anyway and better use same approach for a whole app.

Despite you may use it like a global access point to all application scoped and initialized dependencies, do not overload it with huge initialization logic, better create some other singletons for this purpose and just initialize it from Application class. In a big project consider using DI framework, Dagger the best one, for providing dependencies.

What is the purpose of Application class in Android

Nice question !

Your application is a context that is always running while your activities and services are running.

It is also the first context to be created and the last to be destroyed.
Thus, it surrounds the life cycle of your app.

You can use the application class as a way to share data or components (for dependency injection for instance). For instance if you want to share a singleton between activities, you can create the instance in the application class and provide a getter, then all other contexts can get the singleton via

((cast to your class)getApplicationContext()).getFoo();

There may be some use cases where you need to do stuff before even your first activity is launched, then do it in the onCreate method of the application class.

On the other hand, you should never relie on the onDestroy method of the Application class, as it is not always called. There is no contract for that on Android.

But this is rare and, usually, you don't need to override the application class though. Dependency injection can be achieved in other ways by RoboGuice or Dagger for instance.

Extending Android Application class

I wouldn't bother trying to merge your Constants class with this class. It isn't worth the effort. Just create a new class that does what is necessary for ACRA. Like this:

public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// do the ACRA init here
}
}

Now you need to make sure that your MyApplication class gets used. So you need to add android:name to the <application> tag entry in your manifest like so

<application
android:name="fully.qualified.package.name.MyApplication"

Done.

What is the difference between Extends Application and Extends Activity in Android?

The android.app.Application class is an optional facility for extending and storing application-global state. There are other ways of doing this, so most apps don't customize this class.

Activities however are what defines every major stage of your application. It wouldn't be possible to build an application without Activities. You will have a main Activity class and this will indeed be defined with 'extends Activity'.

Extend Application class when developing aar

The Application class in the greater app should extend your Application subclass instead of Application directly.

Then they should both work the same as before.

If the greater application does not have a Application class, you can just register your subclass in the manifest, or create a dummy class extending yours.



Related Topics



Leave a reply



Submit