From 4a961d5c4256e88f4932eee85d255b875e8bd79a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E7=BF=94?= Date: Thu, 28 Jun 2018 19:16:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Router=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xutildemo/activity/MainActivity.java | 15 ---- .../xutildemo/fragment/MainFragment.java | 16 +++- .../com/xuexiang/xutil/app/router/Router.java | 81 ++++++++++++++----- .../xutil/app/router/RouterCallback.java | 8 +- .../app/router/SimpleRouterCallback.java | 8 +- 5 files changed, 84 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/xuexiang/xutildemo/activity/MainActivity.java b/app/src/main/java/com/xuexiang/xutildemo/activity/MainActivity.java index d66ea56..08f2e2a 100644 --- a/app/src/main/java/com/xuexiang/xutildemo/activity/MainActivity.java +++ b/app/src/main/java/com/xuexiang/xutildemo/activity/MainActivity.java @@ -16,12 +16,9 @@ package com.xuexiang.xutildemo.activity; -import android.content.Intent; import android.os.Bundle; import com.xuexiang.xpage.base.XPageActivity; -import com.xuexiang.xutil.app.IntentUtils; -import com.xuexiang.xutil.tip.ToastUtils; import com.xuexiang.xutildemo.fragment.MainFragment; public class MainActivity extends XPageActivity { @@ -31,16 +28,4 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); openPage(MainFragment.class); } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (resultCode == RESULT_OK) { - toast("请求码:" + requestCode + ", 返回码:" + resultCode + ", 返回内容:" + IntentUtils.getStringExtra(data, "back")); - } - } - - protected void toast(String msg) { - ToastUtils.toast(msg); - } } diff --git a/app/src/main/java/com/xuexiang/xutildemo/fragment/MainFragment.java b/app/src/main/java/com/xuexiang/xutildemo/fragment/MainFragment.java index 789965a..56aa118 100644 --- a/app/src/main/java/com/xuexiang/xutildemo/fragment/MainFragment.java +++ b/app/src/main/java/com/xuexiang/xutildemo/fragment/MainFragment.java @@ -16,6 +16,7 @@ package com.xuexiang.xutildemo.fragment; +import android.content.Intent; import android.util.Log; import android.view.KeyEvent; import android.view.View; @@ -23,16 +24,20 @@ import com.xuexiang.xpage.annotation.Page; import com.xuexiang.xpage.base.XPageSimpleListFragment; import com.xuexiang.xpage.utils.TitleBar; +import com.xuexiang.xutil.app.IntentUtils; import com.xuexiang.xutil.app.SocialShareUtils; import com.xuexiang.xutil.app.router.Router; import com.xuexiang.xutil.common.ClickUtils; import com.xuexiang.xutil.data.DateUtils; +import com.xuexiang.xutil.tip.ToastUtils; import com.xuexiang.xutildemo.activity.TestRouterActivity; import java.util.HashMap; import java.util.List; import java.util.Map; +import static android.app.Activity.RESULT_OK; + /** * 主界面 * @author xuexiang @@ -72,7 +77,7 @@ protected void onItemClick(int position) { // ActivityUtils.startActivityForResult(this, "com.xuexiang.TestRouter", 100); // ActivityUtils.startActivityForResult(this, "com.xuexiang.TestRouter", 100, params); // ActivityUtils.startActivityForResult(this, TestRouterActivity.class, 100, params); - Router.newIntent(getActivity()).to(TestRouterActivity.class).putExtraParam("param1", "我是参数1").requestCode(100).launch(); + Router.newIntent(this).to(TestRouterActivity.class).putExtraParam("param1", "我是参数1").requestCode(100).launch(); Log.e("xuexiang", DateUtils.nDaysAfterToday(2, true)); Log.e("xuexiang", DateUtils.nDaysBeforeToday(2, true)); @@ -110,4 +115,13 @@ public boolean onKeyDown(int keyCode, KeyEvent event) { } return true; } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == RESULT_OK) { + ToastUtils.toast("请求码:" + requestCode + ", 返回码:" + resultCode + ", 返回内容:" + IntentUtils.getStringExtra(data, "back")); + } + } + } diff --git a/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/Router.java b/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/Router.java index b936d44..e8662d8 100644 --- a/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/Router.java +++ b/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/Router.java @@ -17,6 +17,8 @@ package com.xuexiang.xutil.app.router; import android.app.Activity; +import android.app.Fragment; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; @@ -38,7 +40,11 @@ public class Router { public static final int ROUTER_ANIM_EXIT = Router.RES_NONE; private Intent intent; - private Activity from; + private Activity fromActivity; + + private Fragment fromFragment; + private android.support.v4.app.Fragment fromFragmentV4; + private Class to; private Bundle data; private ActivityOptionsCompat options; @@ -56,7 +62,19 @@ private Router() { public static Router newIntent(Activity context) { Router router = new Router(); - router.from = context; + router.fromActivity = context; + return router; + } + + public static Router newIntent(Fragment fragment) { + Router router = new Router(); + router.fromFragment = fragment; + return router; + } + + public static Router newIntent(android.support.v4.app.Fragment fragment) { + Router router = new Router(); + router.fromFragmentV4 = fragment; return router; } @@ -107,46 +125,69 @@ public Router anim(int enterAnim, int exitAnim) { } public void launch() { + Context context = getFromContext(); try { - if (intent != null && from != null && to != null) { - + if (intent != null && context != null && to != null) { if (callback != null) { - callback.onBefore(from, to); + callback.onBefore(context, to); } - - intent.setClass(from, to); - + intent.setClass(context, to); intent.putExtras(getBundleData()); if (options == null) { - if (requestCode < 0) { - from.startActivity(intent); - } else { - from.startActivityForResult(intent, requestCode); - } - + startActivity(); if (enterAnim > 0 && exitAnim > 0) { - from.overridePendingTransition(enterAnim, exitAnim); + ((Activity)context).overridePendingTransition(enterAnim, exitAnim); } } else { if (requestCode < 0) { - ActivityCompat.startActivity(from, intent, options.toBundle()); + ActivityCompat.startActivity(context, intent, options.toBundle()); } else { - ActivityCompat.startActivityForResult(from, intent, requestCode, options.toBundle()); + ActivityCompat.startActivityForResult((Activity) context, intent, requestCode, options.toBundle()); } } - if (callback != null) { - callback.onNext(from, to); + callback.onNext(context, to); } } } catch (Throwable throwable) { if (callback != null) { - callback.onError(from, to, throwable); + callback.onError(context, to, throwable); } } } + private void startActivity() { + if (requestCode < 0) { + if (fromActivity != null) { + fromActivity.startActivity(intent); + } else if (fromFragment != null) { + fromFragment.startActivity(intent); + } else if (fromFragmentV4 != null) { + fromFragmentV4.startActivity(intent); + } + } else { + if (fromActivity != null) { + fromActivity.startActivityForResult(intent, requestCode); + } else if (fromFragment != null) { + fromFragment.startActivityForResult(intent, requestCode); + } else if (fromFragmentV4 != null) { + fromFragmentV4.startActivityForResult(intent, requestCode); + } + } + } + + private Context getFromContext() { + if (fromActivity != null) { + return fromActivity; + } else if (fromFragment != null) { + return fromFragment.getActivity(); + } else if (fromFragmentV4 != null) { + return fromFragmentV4.getContext(); + } + return null; + } + private Bundle getBundleData() { if (data == null) { data = new Bundle(); diff --git a/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/RouterCallback.java b/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/RouterCallback.java index f14b69b..5281eb3 100644 --- a/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/RouterCallback.java +++ b/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/RouterCallback.java @@ -16,7 +16,7 @@ package com.xuexiang.xutil.app.router; -import android.app.Activity; +import android.content.Context; /** *
@@ -27,10 +27,10 @@
  */
 public interface RouterCallback {
 
-    void onBefore(Activity from, Class to);
+    void onBefore(Context from, Class to);
 
-    void onNext(Activity from, Class to);
+    void onNext(Context from, Class to);
 
-    void onError(Activity from, Class to, Throwable throwable);
+    void onError(Context from, Class to, Throwable throwable);
 
 }
diff --git a/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/SimpleRouterCallback.java b/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/SimpleRouterCallback.java
index af44dbe..4775aa9 100644
--- a/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/SimpleRouterCallback.java
+++ b/xutil-sub/src/main/java/com/xuexiang/xutil/app/router/SimpleRouterCallback.java
@@ -16,7 +16,7 @@
 
 package com.xuexiang.xutil.app.router;
 
-import android.app.Activity;
+import android.content.Context;
 
 /**
  * 
@@ -28,17 +28,17 @@
 public class SimpleRouterCallback implements RouterCallback {
 
     @Override
-    public void onBefore(Activity from, Class to) {
+    public void onBefore(Context from, Class to) {
 
     }
 
     @Override
-    public void onNext(Activity from, Class to) {
+    public void onNext(Context from, Class to) {
 
     }
 
     @Override
-    public void onError(Activity from, Class to, Throwable throwable) {
+    public void onError(Context from, Class to, Throwable throwable) {
 
     }
 }