Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android]更新使用示例,修复bug #2148

Open
wants to merge 2 commits into
base: dev/exp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ implementation project(':flutter_boost')
<meta-data android:name="flutterEmbedding"
android:value="2">
</meta-data>


```

然后点击右上角的sync同步一下,就会开始一些下载和同步的进程,等待完成
Expand All @@ -181,23 +179,41 @@ public class App extends Application {
public void onCreate() {
super.onCreate();
FlutterBoost.instance().setup(this, new FlutterBoostDelegate() {

private void startActivityForResult(Intent intent, int requestCode) {
FlutterViewContainer topContainer = FlutterBoost.instance().getTopContainer();
if (topContainer instanceof FlutterFragment) {
//如果是从FlutterBoostFragment唤起的新页面,只有使用FlutterFragment进行start,才能收到result
((FlutterFragment) topContainer).startActivityForResult(intent, requestCode);
} else if (topContainer instanceof FlutterActivity) {
((FlutterActivity) topContainer).startActivityForResult(intent, requestCode);
} else {
FlutterBoost.instance().currentActivity().startActivityForResult(intent,
requestCode);
}
}

@Override
public void pushNativeRoute(FlutterBoostRouteOptions options) {
//这里根据options.pageName来判断你想跳转哪个页面,这里简单给一个
Intent intent = new Intent(FlutterBoost.instance().currentActivity(), YourTargetAcitvity.class);
FlutterBoost.instance().currentActivity().startActivityForResult(intent, options.requestCode());
//TODO 这里的currentActivity在FlutterBoost延迟初始化+第一个页面是FlutterBoostFragment时,可能存在NPE,
//TODO 建议使用自己管理的堆栈的顶层Activity
Context context = FlutterBoost.instance().currentActivity();
Intent intent = new Intent(context, YourTargetAcitvity.class);
startActivityForResult(intent, options.requestCode());
}

@Override
public void pushFlutterRoute(FlutterBoostRouteOptions options) {
Context context = FlutterBoost.instance().currentActivity();
Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(FlutterBoostActivity.class)
.backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
.destroyEngineWithActivity(false)
.uniqueId(options.uniqueId())
.url(options.pageName())
.urlParams(options.arguments())
.build(FlutterBoost.instance().currentActivity());
FlutterBoost.instance().currentActivity().startActivity(intent);
.build(context);
startActivityForResult(intent, options.requestCode();
}
}, engine -> {
});
Expand Down Expand Up @@ -325,21 +341,3 @@ FlutterBoost.instance().setup(application, delegate: delegate) { engine in
```

到此为止,所有的前置内容均已完成


















Original file line number Diff line number Diff line change
@@ -1,42 +1,75 @@
package com.idlefish.flutterboost.example;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostDelegate;
import com.idlefish.flutterboost.FlutterBoostRouteOptions;
import com.idlefish.flutterboost.containers.FlutterBoostActivity;
import com.idlefish.flutterboost.containers.FlutterViewContainer;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterActivityLaunchConfigs.BackgroundMode;
import io.flutter.embedding.android.FlutterFragment;

public class MyFlutterBoostDelegate implements FlutterBoostDelegate {

private Context getCurrentContext() {
FlutterViewContainer topContainer = FlutterBoost.instance().getTopContainer();
Context context = null;
if (topContainer != null) {
context = topContainer.getContextActivity();
}
if (context == null) {
context = FlutterBoost.instance().currentActivity();
}
return context;
}

private void startActivityForResult(Intent intent, int requestCode) {
FlutterViewContainer topContainer = FlutterBoost.instance().getTopContainer();
if (topContainer instanceof FlutterFragment) {
//如果是从FlutterBoostFragment唤起的新页面,只有使用FlutterFragment进行start,才能收到result
((FlutterFragment) topContainer).startActivityForResult(intent, requestCode);
} else if (topContainer instanceof FlutterActivity) {
((FlutterActivity) topContainer).startActivityForResult(intent, requestCode);
} else {
FlutterBoost.instance().currentActivity().startActivityForResult(intent,
requestCode);
}
}

@Override
public void pushNativeRoute(FlutterBoostRouteOptions options) {
Intent intent = new Intent(FlutterBoost.instance().currentActivity(), NativePageActivity.class);
FlutterBoost.instance().currentActivity().startActivityForResult(intent, options.requestCode());
Context context = getCurrentContext();
Intent intent = new Intent(context, NativePageActivity.class);
startActivityForResult(intent, options.requestCode());
}

@Override
public void pushFlutterRoute(FlutterBoostRouteOptions options) {
Class<? extends FlutterBoostActivity> activityClass = options.opaque() ? FlutterBoostActivity.class : TransparencyPageActivity.class;
Class<? extends FlutterBoostActivity> activityClass = options.opaque() ?
FlutterBoostActivity.class : TransparencyPageActivity.class;
Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(activityClass)
.destroyEngineWithActivity(false)
// 注意:这里需要回传dart带过来的uniqueId,否则页面退出时传参可能失败。
// 但,如果是从Native打开Flutter页面,请不要给uniqueId赋*任何值*!!!
.uniqueId(options.uniqueId())
.backgroundMode(options.opaque() ? BackgroundMode.opaque : BackgroundMode.transparent)
.backgroundMode(options.opaque() ? BackgroundMode.opaque :
BackgroundMode.transparent)
.url(options.pageName())
.urlParams(options.arguments())
.build(FlutterBoost.instance().currentActivity());
FlutterBoost.instance().currentActivity().startActivity(intent);
.build(getCurrentContext());
startActivityForResult(intent, options.requestCode());
}

@Override
public boolean popRoute(FlutterBoostRouteOptions options) {
//自定义popRoute处理逻辑,如果不想走默认处理逻辑返回true进行拦截
Toast.makeText(FlutterBoost.instance().currentActivity().getApplicationContext(), "Add customized popRoute handler here", Toast.LENGTH_SHORT).show();
Toast.makeText(FlutterBoost.instance().currentActivity().getApplicationContext(), "Add " +
"customized popRoute handler here", Toast.LENGTH_SHORT).show();
return false;
}
}
9 changes: 8 additions & 1 deletion example/lib/tab/simple_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ class _SimpleWidgetState extends State<SimpleWidget>
'open native page',
style: TextStyle(fontSize: 22.0, color: Colors.black),
)),
onTap: () => BoostNavigator.instance.push("native"),
onTap: () => BoostNavigator.instance.push("native").then(
(value) {
if (mounted) {
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(SnackBar(content: Text("$value")));
}
}),
),
InkWell(
child: Container(
Expand Down