3

I am trying to configure local_auth in my flutter app and I came across following instructions local_auth plugin Android Integration.

local_auth plugin requires the use of a FragmentActivity instead of Activity. This can be easily done by switching FlutterFragmentActivity to FlutterActivity in the manifest (or your own Activity class if you are extending the base class).

Following is my MainActivity class code snippets:

package com.example.xxxxxxxx;
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import com.anggach.flutterandroidlifecycle.FlutterAndroidLifecycleActivity;
import io.flutter.app.FlutterFragmentActivity;

public class MainActivity extends FlutterAndroidLifecycleActivity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

But my problem is that MainActivity class in my code is already extending from FlutterAndroidLifecycleActivity, and I am not able to extend it from two packages.

Some additional details: I tried to resolve it by putting both the classes together but didn't get success because Java doesn't support Multiple Inheritance.

There are other resources about this in Java as:

Extending from two classes

Can one class extend two classes?

To summarise the issue, I need to be able to extend both FlutterFragmentActivity and FlutterAndroidLifecycleActivity from MainActivity

Anil Chauhan
  • 658
  • 6
  • 17

3 Answers3

2

In MainActivity.java:

package com.your.package;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterFragmentActivity {

   @Override
   public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
      GeneratedPluginRegistrant.registerWith(flutterEngine);
   }
}
1

I had same issue here. Thanks Tidder for your suggestion.

I cloned https://github.com/blackmenthor/flutter-android-lifecycle-plugin to my project directory and change Activity to Fragment.

I also make a pull request here: https://github.com/blackmenthor/flutter-android-lifecycle-plugin/pull/3

You can check my changes. Help it will help you.

Tamdesktop
  • 36
  • 3
-1

You can not extend from two or more classes in Java. Go and check the documentation for both of your libraries to see if there are other options (like an interface). Otherwise check the code of both libs to see if you can build your own custom FlutterFragmentActivity that implements both features.

Tidder
  • 1,126
  • 1
  • 7
  • 15