Commit c7fe899c authored by yangwu.jia's avatar yangwu.jia

1.9 demo优化

parent 3c3abf9f
...@@ -31,6 +31,7 @@ import io.flutter.embedding.engine.FlutterShellArgs; ...@@ -31,6 +31,7 @@ import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.plugin.platform.PlatformPlugin; import io.flutter.plugin.platform.PlatformPlugin;
import io.flutter.view.FlutterMain; import io.flutter.view.FlutterMain;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -71,7 +72,7 @@ public class NewBoostFlutterActivity extends Activity ...@@ -71,7 +72,7 @@ public class NewBoostFlutterActivity extends Activity
private final Class<? extends NewBoostFlutterActivity> activityClass; private final Class<? extends NewBoostFlutterActivity> activityClass;
private String backgroundMode = DEFAULT_BACKGROUND_MODE; private String backgroundMode = DEFAULT_BACKGROUND_MODE;
private String url = ""; private String url = "";
private HashMap params = new HashMap(); private Map params = new HashMap();
...@@ -88,7 +89,7 @@ public class NewBoostFlutterActivity extends Activity ...@@ -88,7 +89,7 @@ public class NewBoostFlutterActivity extends Activity
} }
public NewEngineIntentBuilder params (@NonNull HashMap params) { public NewEngineIntentBuilder params (@NonNull Map params) {
this.params = params; this.params = params;
return this; return this;
} }
...@@ -102,15 +103,30 @@ public class NewBoostFlutterActivity extends Activity ...@@ -102,15 +103,30 @@ public class NewBoostFlutterActivity extends Activity
public Intent build(@NonNull Context context) { public Intent build(@NonNull Context context) {
SerializableMap serializableMap=new SerializableMap();
serializableMap.setMap(params);
return new Intent(context, activityClass) return new Intent(context, activityClass)
.putExtra(EXTRA_BACKGROUND_MODE, backgroundMode) .putExtra(EXTRA_BACKGROUND_MODE, backgroundMode)
.putExtra(EXTRA_DESTROY_ENGINE_WITH_ACTIVITY, false) .putExtra(EXTRA_DESTROY_ENGINE_WITH_ACTIVITY, false)
.putExtra(EXTRA_URL, url) .putExtra(EXTRA_URL, url)
.putExtra(EXTRA_PARAMS, (HashMap)params); .putExtra(EXTRA_PARAMS, serializableMap);
} }
} }
public static class SerializableMap implements Serializable {
private Map<String,Object> map;
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
private FlutterActivityAndFragmentDelegate delegate; private FlutterActivityAndFragmentDelegate delegate;
...@@ -476,7 +492,8 @@ public class NewBoostFlutterActivity extends Activity ...@@ -476,7 +492,8 @@ public class NewBoostFlutterActivity extends Activity
public Map getContainerUrlParams() { public Map getContainerUrlParams() {
if (getIntent().hasExtra(EXTRA_PARAMS)) { if (getIntent().hasExtra(EXTRA_PARAMS)) {
return (Map) getIntent().getSerializableExtra(EXTRA_PARAMS); SerializableMap serializableMap= (SerializableMap)getIntent().getSerializableExtra(EXTRA_PARAMS);
return serializableMap.getMap();
} }
Map<String,String> params = new HashMap<>(); Map<String,String> params = new HashMap<>();
......
...@@ -115,7 +115,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr ...@@ -115,7 +115,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
private FlutterView.TransparencyMode transparencyMode = FlutterView.TransparencyMode.transparent; private FlutterView.TransparencyMode transparencyMode = FlutterView.TransparencyMode.transparent;
private boolean shouldAttachEngineToActivity = true; private boolean shouldAttachEngineToActivity = true;
private String url = ""; private String url = "";
private HashMap params = new HashMap(); private Map params = new HashMap();
/** /**
* Constructs a {@code NewEngineFragmentBuilder} that is configured to construct an instance of * Constructs a {@code NewEngineFragmentBuilder} that is configured to construct an instance of
* {@code NewFlutterFragment}. * {@code NewFlutterFragment}.
...@@ -162,7 +162,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr ...@@ -162,7 +162,7 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
} }
public NewEngineFragmentBuilder params (@NonNull HashMap params) { public NewEngineFragmentBuilder params (@NonNull Map params) {
this.params = params; this.params = params;
return this; return this;
} }
...@@ -188,11 +188,17 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr ...@@ -188,11 +188,17 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
if (null != shellArgs) { if (null != shellArgs) {
args.putStringArray(ARG_FLUTTER_INITIALIZATION_ARGS, shellArgs.toArray()); args.putStringArray(ARG_FLUTTER_INITIALIZATION_ARGS, shellArgs.toArray());
} }
NewBoostFlutterActivity.SerializableMap serializableMap=new NewBoostFlutterActivity.SerializableMap();
serializableMap.setMap(params);
args.putString(EXTRA_URL, url); args.putString(EXTRA_URL, url);
args.putSerializable(EXTRA_PARAMS, (HashMap)params); args.putSerializable(EXTRA_PARAMS, serializableMap);
args.putString(ARG_FLUTTERVIEW_RENDER_MODE, renderMode != null ? renderMode.name() : FlutterView.RenderMode.surface.name()); args.putString(ARG_FLUTTERVIEW_RENDER_MODE, renderMode != null ? renderMode.name() : FlutterView.RenderMode.surface.name());
args.putString(ARG_FLUTTERVIEW_TRANSPARENCY_MODE, transparencyMode != null ? transparencyMode.name() : FlutterView.TransparencyMode.transparent.name()); args.putString(ARG_FLUTTERVIEW_TRANSPARENCY_MODE, transparencyMode != null ? transparencyMode.name() : FlutterView.TransparencyMode.transparent.name());
args.putBoolean(ARG_DESTROY_ENGINE_WITH_FRAGMENT, true); args.putBoolean(ARG_DESTROY_ENGINE_WITH_FRAGMENT, true);
return args; return args;
} }
...@@ -532,7 +538,10 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr ...@@ -532,7 +538,10 @@ public class NewFlutterFragment extends Fragment implements FlutterActivityAndFr
@Override @Override
public Map getContainerUrlParams() { public Map getContainerUrlParams() {
return (HashMap) getArguments().getSerializable(EXTRA_PARAMS);
NewBoostFlutterActivity.SerializableMap serializableMap= (NewBoostFlutterActivity.SerializableMap) getArguments().getSerializable(EXTRA_PARAMS);
return serializableMap.getMap();
} }
/** /**
......
...@@ -45,6 +45,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe ...@@ -45,6 +45,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Map params = new HashMap(); Map params = new HashMap();
params.put("test1","v_test1");
params.put("test2","v_test2");
//Add some params if needed. //Add some params if needed.
if (v == mOpenNative) { if (v == mOpenNative) {
PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL , params); PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL , params);
......
...@@ -33,6 +33,9 @@ public class NativePageActivity extends AppCompatActivity implements View.OnClic ...@@ -33,6 +33,9 @@ public class NativePageActivity extends AppCompatActivity implements View.OnClic
@Override @Override
public void onClick(View v) { public void onClick(View v) {
Map params = new HashMap(); Map params = new HashMap();
params.put("test1","v_test1");
params.put("test2","v_test2");
if (v == mOpenNative) { if (v == mOpenNative) {
PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL,params); PageRouter.openPageByUrl(this, PageRouter.NATIVE_PAGE_URL,params);
} else if (v == mOpenFlutter) { } else if (v == mOpenFlutter) {
......
...@@ -3,6 +3,7 @@ package com.taobao.idlefish.flutterboostexample; ...@@ -3,6 +3,7 @@ package com.taobao.idlefish.flutterboostexample;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import com.idlefish.flutterboost.containers.NewBoostFlutterActivity; import com.idlefish.flutterboost.containers.NewBoostFlutterActivity;
import java.util.HashMap; import java.util.HashMap;
...@@ -10,24 +11,34 @@ import java.util.Map; ...@@ -10,24 +11,34 @@ import java.util.Map;
public class PageRouter { public class PageRouter {
public final static Map<String, String> pageName = new HashMap<String, String>() {{
put("sample://flutterPage1", "flutterPage1");
put("sample://flutterPage2", "flutterPage2");
put("sample://flutterPage3", "flutterPage3");
put("sample://flutterMain", "flutterMain");
}};
public static final String NATIVE_PAGE_URL = "sample://nativePage"; public static final String NATIVE_PAGE_URL = "sample://nativePage";
public static final String FLUTTER_PAGE_URL = "sample://flutterPage"; public static final String FLUTTER_PAGE_URL = "sample://flutterMain";
public static final String FLUTTER_FRAGMENT_PAGE_URL = "sample://flutterFragmentPage"; public static final String FLUTTER_FRAGMENT_PAGE_URL = "sample://flutterFragmentPage";
public static boolean openPageByUrl(Context context, String url,Map params) { public static boolean openPageByUrl(Context context, String url, Map params) {
return openPageByUrl(context, url,params, 0); return openPageByUrl(context, url, params, 0);
} }
public static boolean openPageByUrl(Context context, String url, Map params, int requestCode) { public static boolean openPageByUrl(Context context, String url, Map params, int requestCode) {
try {
if (url.startsWith(FLUTTER_PAGE_URL)) {
HashMap p=new HashMap();
Intent intent= NewBoostFlutterActivity.withNewEngine().url("flutterPage").params(p) String path = url.split("\\?")[0];
Log.i("openPageByUrl",path);
try {
if (pageName.containsKey(path)) {
Intent intent = NewBoostFlutterActivity.withNewEngine().url(pageName.get(path)).params(params)
.backgroundMode(NewBoostFlutterActivity.BackgroundMode.opaque).build(context); .backgroundMode(NewBoostFlutterActivity.BackgroundMode.opaque).build(context);
context.startActivity( intent); context.startActivity(intent);
return true;
} else if (url.startsWith(FLUTTER_FRAGMENT_PAGE_URL)) { } else if (url.startsWith(FLUTTER_FRAGMENT_PAGE_URL)) {
context.startActivity(new Intent(context, FlutterFragmentPageActivity.class)); context.startActivity(new Intent(context, FlutterFragmentPageActivity.class));
return true; return true;
...@@ -40,5 +51,6 @@ public class PageRouter { ...@@ -40,5 +51,6 @@ public class PageRouter {
} catch (Throwable t) { } catch (Throwable t) {
return false; return false;
} }
return false;
} }
} }
...@@ -17,16 +17,15 @@ class _MyAppState extends State<MyApp> { ...@@ -17,16 +17,15 @@ class _MyAppState extends State<MyApp> {
super.initState(); super.initState();
FlutterBoost.singleton.registerPageBuilders({ FlutterBoost.singleton.registerPageBuilders({
'first': (pageName, params, _) => FirstRouteWidget(), 'flutterPage1': (pageName, params, _) => FirstRouteWidget(),
'second': (pageName, params, _) => SecondRouteWidget(), 'flutterPage2': (pageName, params, _) => SecondRouteWidget(),
'tab': (pageName, params, _) => TabRouteWidget(), 'flutterPage3': (pageName, params, _) => TabRouteWidget(),
'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params), 'flutterFragment': (pageName, params, _) => FragmentRouteWidget(params),
///可以在native层通过 getContainerParams 来传递参数 ///可以在native层通过 getContainerParams 来传递参数
'flutterPage': (pageName, params, _) { 'flutterMain': (pageName, params, _) {
print("flutterPage params:$params"); print("flutterPage params:$params");
return FlutterRouteWidget(); return FlutterRouteWidget(params:params);
}, },
}); });
} }
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment