Home > Back-end >  Merging Activities in kotlin
Merging Activities in kotlin

Time:01-01

I have three different activities which extends three different things and i want to implement multi level inheritance so that i can add it in android:name of androidmanifest.

Here are all activities. I don't know native development at all. Any help would be appreciated.

1st activity

package com.package
import hrx.plugin.monet.MonetApplication
class CustomApplication : MonetApplication()

2nd activity

package com.package
import io.flutter.embedding.android.FlutterActivity
class MainActivity: FlutterActivity() {
}

3rd activity

package com.package;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.androidalarmmanager.AlarmService;
class Application : FlutterApplication(), PluginRegistrantCallback {
      override fun onCreate() {
        super.onCreate();
        AlarmService.setPluginRegistrant(this);
      }
      override fun registerWith(registry: PluginRegistry) {
        GeneratedPluginRegistrant.registerWith(registry);
      }
}

CodePudding user response:

I have three different activities

Actually, you seem to have two different Application objects and one Activity. If you don't know the difference you may want to go back and take some Android 101 courses.

i want to implement multi level inheritance so that i can add it in android:name of androidmanifest.

OK. Well you have

class CustomApplication : MonetApplication()

and

class Application : FlutterApplication(), PluginRegistrantCallback

Looking at the definition of MonetApplication we see that it's defined as

open class MonetApplication : FlutterApplication()

So MonetApplication is a FlutterApplication so if you extend MonetApplication you are effectively also extending FlutterApplication, so you can basically merge your two existing classes into one:

class HighlanderApplication : MonetApplication(), PluginRegistrantCallback {
      override fun onCreate() {
        super.onCreate();
        AlarmService.setPluginRegistrant(this);
      }
      override fun registerWith(registry: PluginRegistry) {
        GeneratedPluginRegistrant.registerWith(registry);
      }
}

Now you can use HighlanderApplication in your manifest and get all the logic that was in both.

  • Related