Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Flutter add2app scenario How do I get the app back button to return to host app?

I am using flutter in an add2app scenario where a the host app starts a flutterView and flutter view is a flutter module as in https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps

I have an activity with a fragment in it. the fragment has

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    FlutterView flutterView = Flutter.createView(getActivity(), getLifecycle(), route);
    GeneratedPluginRegistrant.registerWith(flutterView.getPluginRegistry());

return flutterView;

I am wondering what the best wash is to make it so the dart code in the app bar can have a back button in the upper left that achieves the effect of super.onBackPressed in the host fragment and its activity - i.e. so the upper left back button behaves the same way as the hardware back button, returning you to the previous screen in the backstack in the host app.

I have tried

appBar: AppBar(
    leading: BackButton()
    title: const Text("My Screen Title),
    ),

but as expected since it is initialRoute, then canPop=false. I also tried a custom BackButton() which was identical except instead of

return IconButton(
  icon: const BackButtonIcon(),
  color: color,
  tooltip: MaterialLocalizations.of(context).backButtonTooltip,
  onPressed: () {
    Navigator.maybePop(context);
  }
);

like back_button.dart it uses

Navigator.Pop(context);

instead of

Navigator.maybePop(context);

Not completely surprisingly it causes a blank black screen instead of returning to Android platform java code.

I expect I can and probably will use a platform call to do fragment.getActivity().finish() but I'm wondering if there's a better way yet because this seems fairly fundamental.

Thanks in advance

like image 359
nAndroid Avatar asked Jan 30 '19 22:01

nAndroid


People also ask

How to replace app bar back button Android iOS in flutter?

By default the back arrow button is fixed in every flutter android iOS application but we can easily change it by using custom Icons or widgets. In today’s tutorial we are replacing the current app bar icon with new icon from Icon library. So in this tutorial we would Flutter Replace Override App Bar Back Button Android iOS Example Tutorial.

How to show back button in flutter?

How to Show Back Button in Flutter. Step 1: Create Flutter application remove all code and make main.dart file like below. Step 2: Create a widget to show back button. Step 3 : Add Created widget to main.dart file The complete code will like below.

What is app bar back button in Android?

App Bar back button is base of every mobile application which has multiple activities screens. Using the App Bar back button mobile UI provides simple navigation to their app users to go back to previous screen.


2 Answers

I know the post is old, but I was just looking for the same thing and found out this can be done from the Dart code by calling:

SystemNavigator.pop();

It is mentioned in the Flutter iOS add-to-app doc but it also works on Android.

like image 179
Digoun Avatar answered Oct 15 '22 10:10

Digoun


Another way to view this problem is: How can I send a message from my Flutter app to instruct Android to take some action.

In this case what you want is to tap on a back button in Flutter to cause Android to finish() the current Activity, or to instruct a containing Fragment to take some custom navigation action.

Message passing between Dart and Android is accomplished by using platform channels. The following guide describes how to setup and use platform channels:

https://flutter.io/docs/development/platform-integration/platform-channels

So I would start with something in Flutter like the following:

MaterialApp(
  appBar: AppBar(
    leading: IconButton(
      icon: Icons.back,
      onPressed: () {
        // Send your platform channel message, or invoke some other method that will.
      }
    ),
    title: const Text("My Screen Title),
  ),
);
like image 28
SuperDeclarative Avatar answered Oct 15 '22 09:10

SuperDeclarative