Skip to content

Commit

Permalink
完善Router代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexiangjys committed Jun 28, 2018
1 parent 1bfaa71 commit 4a961d5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@

package com.xuexiang.xutildemo.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

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
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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"));
}
}

}
81 changes: 61 additions & 20 deletions xutil-sub/src/main/java/com/xuexiang/xutil/app/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.xuexiang.xutil.app.router;

import android.app.Activity;
import android.content.Context;

/**
* <pre>
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.xuexiang.xutil.app.router;

import android.app.Activity;
import android.content.Context;

/**
* <pre>
Expand All @@ -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) {

}
}

0 comments on commit 4a961d5

Please sign in to comment.