Home > front end >  More detailed explanation on what the <application> in the app manifest file do? (Android kotl
More detailed explanation on what the <application> in the app manifest file do? (Android kotl

Time:10-24

What is the significance of declaring a name attribute in the <application> section of the android manifest file?

I am trying to figure out what does specifying the ".BaseApplication" for the name attribute even do? From what I have read all it does is instantiate the BaseApplication class first(BaseApplication needs to inherit from Application).

Other than that what other utility does it provide?

Android Manifest File

<application
    android:name = ".BaseApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Forage">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.Forage.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

BaseApplication Class

class BaseApplication : Application() {
val database:ForageDatabase by lazy {
    ForageDatabase.getdatabase(this)

}    
}

Instantiating of the BaseApplication class

 private val viewModel: ForageableViewModel by activityViewModels {
    ViewModelFactory(
        (activity?.application as BaseApplication).database.ForageableDoa())
}

CodePudding user response:

activity?.application doesn't instantiate an application. It is just returning the reference to the already existing Application. The Application class is the first thing instantiated when Android starts up your app process. There is only ever one instance of it for the lifetime of your application session.

And that's the reason you might use a custom Application subclass instead of the default Application. When you name a specific Application subclass in your manifest, then when Android starts your app, it will use that subclass instead of Application. Then you can use Application lifecycle hooks like onCreate() to set up things you want to be available throughout your app for its whole lifecycle.

In your example, there is a database being instantiated in the Application. Typically, you only want a single instance of a database for the entire app, so by creating it in the Application, you ensure that there is only ever one instance of it, and it is guaranteed to be available from anywhere in your app at any time.

Another example would be a logging library. There is a popular Android logging library called Timber that has more features than the built-in logging. In order to use it, you call functions on Timber's companion object, but Timber needs to be set up before you can use it. By setting it up in your Application subclass's onCreate(), you ensure that it is set up before any other code in your app could possibly try to use it.

  • Related