Skip to content

Commit

Permalink
feat(feat): use default Context
Browse files Browse the repository at this point in the history
if user don't pass Context or Fragment to Router, Router will use Application and add 'NEW_TASK_FLAG', if you pass Application to Router, don't forget add add 'NEW_TASK_FLAG' by call 'addIntentFalg' method
  • Loading branch information
keepactive committed Jul 6, 2019
1 parent 4467d55 commit bb5c42b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ private void implementInterfaceMethod(TypeSpec.Builder typeSpecBuilder, Executab
} else if (fragmentParameter != null) {
args.add(fragmentParameter.getSimpleName().toString());
} else {
throw new ProcessException("do you forget to add a 'Context' or 'Activity' or 'android.support.v4.app.Fragment' parameter to method(" + methodPath + ") ?");
args.add("");
// throw new ProcessException("do you forget to add a 'Context' or 'Activity' or 'android.support.v4.app.Fragment' parameter to method(" + methodPath + ") ?");
}

// host 和 path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.support.v4.app.FragmentManager;
import android.util.SparseArray;

import com.xiaojinzi.component.Component;
import com.xiaojinzi.component.ComponentUtil;
import com.xiaojinzi.component.RouterRxFragment;
import com.xiaojinzi.component.bean.ActivityResult;
Expand All @@ -22,11 +23,11 @@
import com.xiaojinzi.component.error.ignore.NavigationFailException;
import com.xiaojinzi.component.impl.interceptor.InterceptorCenter;
import com.xiaojinzi.component.impl.interceptor.OpenOnceInterceptor;
import com.xiaojinzi.component.support.RouterInterceptorCache;
import com.xiaojinzi.component.support.Action;
import com.xiaojinzi.component.support.CallbackAdapter;
import com.xiaojinzi.component.support.Consumer;
import com.xiaojinzi.component.support.NavigationDisposable;
import com.xiaojinzi.component.support.RouterInterceptorCache;
import com.xiaojinzi.component.support.Utils;

import java.io.Serializable;
Expand Down Expand Up @@ -64,6 +65,9 @@ public class Navigator extends RouterRequest.Builder implements Call {
*/
protected boolean isFinish = false;

public Navigator() {
}

public Navigator(@NonNull Context context) {
Utils.checkNullPointer(context, "context");
context(context);
Expand Down Expand Up @@ -371,6 +375,14 @@ public RouterRequest build() {
return Help.randomlyGenerateRequestCode(super.build());
}

private void useDefaultApplication(){
// 如果 Context 和 Fragment 都是空的,使用默认的 Application
if (context == null && fragment == null) {
context = Component.getApplication();
addIntentFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}

/**
* 路由前的检查
*
Expand All @@ -397,7 +409,7 @@ private void onCheck() {
*/
private void onCheckForResult() throws Exception {
if (context == null && fragment == null) {
throw new NavigationFailException(new NullPointerException("Context or Fragment is necessary for router"));
throw new NavigationFailException(new NullPointerException("Context or Fragment is necessary for router if you want get ActivityResult"));
}
// 如果是使用 Context 的,那么就必须是 FragmentActivity,需要操作 Fragment
// 这里的 context != null 判断条件不能去掉,不然使用 Fragment 跳转的就过不去了
Expand Down Expand Up @@ -648,6 +660,8 @@ public synchronized NavigationDisposable navigate(@Nullable final Callback callb
// 构建请求对象
RouterRequest originalRequest = null;
try {
// 如果用户没填写 Context 或者 Fragment 默认使用 Application
useDefaultApplication();
// 路由前的检查
onCheck();
// 标记这个 builder 已经不能使用了
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public static void unregister(@NonNull String host) {
RouterCenter.getInstance().unregister(host);
}

public static Navigator with(){
return new Navigator();
}

public static Navigator with(@NonNull Context context) {
return new Navigator(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class RxRouter extends Router {

public static final String TAG = "RxRouter";

public static Builder with() {
return new Builder();
}

/**
* 这个方法父类也有一个静态的,但是父类返回的是 {@link Navigator} 而这个返回的是
* {@link RxRouter.Builder}
Expand All @@ -75,6 +79,10 @@ public static Builder with(@NonNull Fragment fragment) {

public static class Builder extends Navigator {

private Builder() {
super();
}

private Builder(@NonNull Context context) {
super(context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xiaojinzi.base.router;

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

import com.xiaojinzi.base.ModuleConfig;
Expand All @@ -15,10 +14,10 @@
public interface AppApi {

@PathAnno(ModuleConfig.App.TEST_ROUTER)
void goToTestRouter(Activity context);
void goToTestRouter();

@PathAnno(ModuleConfig.App.TEST_QUALITY)
Completable goToTestQuality(Context context);
Completable goToTestQuality();

@HostAnno(ModuleConfig.Help.NAME)
@PathAnno((ModuleConfig.Help.TEST_WEB_ROUTER))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.xiaojinzi.base.router;

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

Expand All @@ -11,9 +10,7 @@
import com.xiaojinzi.component.anno.router.PathAnno;
import com.xiaojinzi.component.anno.router.RequestCodeAnno;
import com.xiaojinzi.component.anno.router.RouterApiAnno;
import com.xiaojinzi.component.bean.ActivityResult;
import com.xiaojinzi.component.impl.BiCallback;
import com.xiaojinzi.component.support.NavigationDisposable;

@RouterApiAnno()
@HostAnno(ModuleConfig.Module1.NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import com.xiaojinzi.base.InterceptorConfig;
import com.xiaojinzi.base.ModuleConfig;
Expand Down Expand Up @@ -61,8 +60,7 @@ Navigator test(Context context, @ParameterAnno("data") String data, Callback cal
@NavigateAnno
@HostAnno(ModuleConfig.Module2.NAME)
@PathAnno(ModuleConfig.Module1.TEST)
void test1(Fragment fragment,
@ParameterAnno("data") String data);
void test1(@ParameterAnno("data") String data);

@NavigateAnno(forResult = true)
@HostAndPathAnno(ModuleConfig.Module1.NAME + "/" + ModuleConfig.Module1.TEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void unloadModule2Module(View v) {
}

public void testRouter(View view) {
Router.withApi(AppApi.class).goToTestRouter(this);
Router.withApi(AppApi.class).goToTestRouter();
}

public void testWebRouter(View view) {
Expand All @@ -76,9 +76,7 @@ public void testWebRouter(View view) {
}

public void testQuality(View view) {
Router.withApi(AppApi.class)
.goToTestQuality(this)
.subscribe();
Router.withApi(AppApi.class).goToTestQuality().subscribe();
}

@Override
Expand Down

0 comments on commit bb5c42b

Please sign in to comment.