add navigator demo for Android and single activity multi fragment
This commit is contained in:
@@ -19,7 +19,6 @@ import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
/**
|
||||
* @Description: pub.doric.demo
|
||||
@@ -27,19 +26,28 @@ import androidx.fragment.app.Fragment;
|
||||
* @CreateDate: 2019-11-19
|
||||
*/
|
||||
public class DoricActivity extends AppCompatActivity {
|
||||
private DoricFragment doricFragment;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.doric_activity);
|
||||
if (savedInstanceState == null) {
|
||||
Fragment doricFragment = getSupportFragmentManager().getFragmentFactory().instantiate(
|
||||
getClassLoader(),
|
||||
DoricFragment.class.getName()
|
||||
);
|
||||
String scheme = getIntent().getStringExtra("scheme");
|
||||
String alias = getIntent().getStringExtra("alias");
|
||||
doricFragment = DoricFragment.newInstance(scheme, alias);
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add(R.id.container, doricFragment)
|
||||
.commit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (doricFragment.canPop()) {
|
||||
doricFragment.pop();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import pub.doric.async.AsyncResult;
|
||||
import pub.doric.navigator.IDoricNavigator;
|
||||
import pub.doric.plugin.DoricJavaPlugin;
|
||||
import pub.doric.shader.RootNode;
|
||||
import pub.doric.utils.DoricConstant;
|
||||
@@ -148,4 +149,14 @@ public class DoricContext {
|
||||
public void onHidden() {
|
||||
callEntity(DoricConstant.DORIC_ENTITY_HIDDEN);
|
||||
}
|
||||
|
||||
private IDoricNavigator doricNavigator;
|
||||
|
||||
public void setDoricNavigator(IDoricNavigator doricNavigator) {
|
||||
this.doricNavigator = doricNavigator;
|
||||
}
|
||||
|
||||
public IDoricNavigator getDoricNavigator() {
|
||||
return this.doricNavigator;
|
||||
}
|
||||
}
|
||||
|
@@ -19,32 +19,66 @@ import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import pub.doric.navigator.IDoricNavigator;
|
||||
|
||||
/**
|
||||
* @Description: pub.doric
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2019-11-23
|
||||
*/
|
||||
public class DoricFragment extends Fragment {
|
||||
private FrameLayout root;
|
||||
public class DoricFragment extends Fragment implements IDoricNavigator {
|
||||
|
||||
public static DoricFragment newInstance(String scheme, String alias) {
|
||||
Bundle args = new Bundle();
|
||||
args.putString("scheme", scheme);
|
||||
args.putString("alias", alias);
|
||||
DoricFragment fragment = new DoricFragment();
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
root = (FrameLayout) inflater.inflate(R.layout.doric_fragment, container, false);
|
||||
return root;
|
||||
return inflater.inflate(R.layout.doric_fragment, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
requireFragmentManager().beginTransaction()
|
||||
.add(R.id.root, DoricPanelFragment.newInstance("assets://demo/Counter.js", "Counter.js"))
|
||||
Bundle argument = getArguments();
|
||||
if (argument != null) {
|
||||
String alias = argument.getString("alias");
|
||||
String scheme = argument.getString("scheme");
|
||||
push(scheme, alias);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(String scheme, String alias) {
|
||||
getChildFragmentManager().beginTransaction()
|
||||
.add(R.id.root, DoricPanelFragment.newInstance(scheme, alias))
|
||||
.addToBackStack(scheme)
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pop() {
|
||||
if (canPop()) {
|
||||
getChildFragmentManager().popBackStack();
|
||||
} else {
|
||||
if (getActivity() != null) {
|
||||
getActivity().finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canPop() {
|
||||
return getChildFragmentManager().getBackStackEntryCount() > 1;
|
||||
}
|
||||
}
|
||||
|
@@ -16,7 +16,6 @@
|
||||
package pub.doric;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@@ -27,6 +26,7 @@ import androidx.fragment.app.Fragment;
|
||||
|
||||
import pub.doric.async.AsyncResult;
|
||||
import pub.doric.loader.DoricJSLoaderManager;
|
||||
import pub.doric.navigator.IDoricNavigator;
|
||||
import pub.doric.utils.DoricLog;
|
||||
|
||||
/**
|
||||
@@ -67,6 +67,11 @@ public class DoricPanelFragment extends Fragment {
|
||||
@Override
|
||||
public void onResult(String result) {
|
||||
doricPanel.config(result, alias);
|
||||
DoricContext context = doricPanel.getDoricContext();
|
||||
Fragment fragment = getParentFragment();
|
||||
if (fragment instanceof IDoricNavigator) {
|
||||
context.setDoricNavigator((IDoricNavigator) fragment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright [2019] [Doric.Pub]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package pub.doric.navigator;
|
||||
|
||||
/**
|
||||
* @Description: pub.doric.navigator
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2019-11-23
|
||||
*/
|
||||
public interface IDoricNavigator {
|
||||
void push(String scheme, String alias);
|
||||
|
||||
void pop();
|
||||
}
|
@@ -50,7 +50,7 @@ public class ModalPlugin extends DoricJavaPlugin {
|
||||
}
|
||||
|
||||
@DoricMethod(thread = ThreadMode.UI)
|
||||
public void toast(JSDecoder decoder, DoricPromise promise) {
|
||||
public void toast(JSDecoder decoder) {
|
||||
try {
|
||||
JSObject jsObject = decoder.decode().asObject();
|
||||
String msg = jsObject.getProperty("msg").asString().value();
|
||||
|
@@ -15,8 +15,15 @@
|
||||
*/
|
||||
package pub.doric.plugin;
|
||||
|
||||
import com.github.pengfeizhou.jscore.ArchiveException;
|
||||
import com.github.pengfeizhou.jscore.JSDecoder;
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.extension.bridge.DoricMethod;
|
||||
import pub.doric.extension.bridge.DoricPlugin;
|
||||
import pub.doric.navigator.IDoricNavigator;
|
||||
import pub.doric.utils.ThreadMode;
|
||||
|
||||
/**
|
||||
* @Description: pub.doric.plugin
|
||||
@@ -28,4 +35,27 @@ public class NavigatorPlugin extends DoricJavaPlugin {
|
||||
public NavigatorPlugin(DoricContext doricContext) {
|
||||
super(doricContext);
|
||||
}
|
||||
|
||||
@DoricMethod(thread = ThreadMode.UI)
|
||||
public void push(JSDecoder jsDecoder) {
|
||||
IDoricNavigator navigator = getDoricContext().getDoricNavigator();
|
||||
if (navigator != null) {
|
||||
try {
|
||||
JSObject jsObject = jsDecoder.decode().asObject();
|
||||
navigator.push(jsObject.getProperty("scheme").asString().value(),
|
||||
jsObject.getProperty("alias").asString().value()
|
||||
);
|
||||
} catch (ArchiveException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@DoricMethod(thread = ThreadMode.UI)
|
||||
public void pop() {
|
||||
IDoricNavigator navigator = getDoricContext().getDoricNavigator();
|
||||
if (navigator != null) {
|
||||
navigator.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<pub.doric.DoricPanel xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
android:layout_height="match_parent"
|
||||
android:background="#ffffff" />
|
Reference in New Issue
Block a user