Lifecycle of flutter as module in Android project

525 views
Skip to first unread message

ZP L

unread,
Aug 30, 2018, 5:56:30 AM8/30/18
to Flutter Dev
Followed official doc here: https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps, I use a global FlutterView, each time an new FlutterStarter is created, FlutterView is attached to this, of course in OnDestroy of FlutterStart, detach is called.

Problem is that, when the 'create flutter' button is
1. pressed on the first time, view pops up, and it goes back to previous Android Activity when Back key pressed.
2. pressed on the second time, view pops up, and it quit the whole Application when Back key pressed.

It seems that something wrong with lifecycle management, no clue, here's my java code, please help.

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;

import io.flutter.app.FlutterFragmentActivity;
import io.flutter.facade.Flutter;
import io.flutter.view.FlutterView;

import android.content.Intent;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.app.FragmentTransaction;
import android.util.Log;


public class FlutterStarter extends FragmentActivity {
   
private static FlutterView fView = null;

   
@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);

       
Intent i = getIntent();
       
String arg = i.getStringExtra("arg");
       
Log.i("FlutterStart", "arg:" + arg);


       
if (fView == null) {
           
fView = Flutter.createView(
                   
this,
                    getLifecycle
(),
                    arg
           
);
       
} else {
           
fView.getFlutterNativeView().attachViewAndActivity(fView, this);
       
}

       
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(-1, -1);
       
//layout.leftMargin = 100;
        //layout.topMargin = 200;
        addContentView(fView, layout);
   
}

   
@Override
    protected void onDestroy() {
       
super.onDestroy();
       
fView.detach();
   
}
}


Matt Carroll

unread,
Aug 30, 2018, 3:29:12 PM8/30/18
to flyi...@gmail.com, Flutter Dev
Is there another Activity that launches your FlutterStarter Activity?  If so, can you include that code?

Quitting the application on back press suggests that your Android backstack only has 1 Activity in it, but it's not clear to me how that may have happened with only the code you sent.

Are you working with Fragments at all?  Or only Activitys?

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ZP L

unread,
Aug 31, 2018, 11:27:41 PM8/31/18
to mattc...@google.com, flutt...@googlegroups.com
Yes. 

package x.x.x;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import io.dcloud.common.DHInterface.IWebview;
import io.dcloud.common.DHInterface.StandardFeature;
import com.tencent.bugly.crashreport.CrashReport;

import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.facade.*;
import android.widget.FrameLayout;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.FragmentTransaction;
import android.util.Log;


public class FlutterEntry extends StandardFeature{
public void onStart(Context pContext, Bundle pSavedInstanceState, String[] pRuntimeArgs) {

}

public void StartView(IWebview pWebview, JSONArray array) {
Intent i = new Intent(pWebview.getActivity(), FlutterStarter.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("arg", array.optString(0));

pWebview.getContext().startActivity(i);
}
}

My application use MUI: http://dev.dcloud.net.cn/mui/, StartView() is called from javascript.

Matt Carroll <mattc...@google.com> 于2018年8月31日周五 上午3:29写道:


--
--刘志平

ZP L

unread,
Sep 3, 2018, 4:31:59 AM9/3/18
to Flutter Dev
Another question:

Flutter.createView takes initialRoute argument, how to go to another route while flutter view already created?


在 2018年9月1日星期六 UTC+8上午11:27:41,ZP L写道:
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
--刘志平

ZP L

unread,
Sep 3, 2018, 4:37:41 AM9/3/18
to flutt...@googlegroups.com
already found FlutterView.pushRoute.


ZP L <flyi...@gmail.com> 于2018年9月3日周一 下午4:32写道:
Another question:

To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
--刘志平

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
--刘志平

Matt Carroll

unread,
Sep 4, 2018, 1:24:04 AM9/4/18
to ZP L, Flutter Dev
I'm not familiar with MUI, and it's not clear to me what is happening when you launch the Flutter Activity.

StandardFeature and IWebView are not Android APIs, as far as I know.  

I would recommend that you try to recreate your situation using only Android APIs so that you can determine whether the problem is Flutter, or whether the problem is this other API that you're using.  If the problem persists, then we can troubleshoot the regular Android code that you used.
Reply all
Reply to author
Forward
0 new messages