Skip to content

Commit

Permalink
feature: put context into postcard.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhi1ong committed Oct 21, 2020
1 parent 94e5b45 commit 633526f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,10 @@
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static Activity activity;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

activity = this;
}

public static Activity getThis() {
return activity;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
*/
@Interceptor(priority = 7)
public class Test1Interceptor implements IInterceptor {
Context mContext;

/**
* The operation of this interceptor.
*
Expand All @@ -35,7 +33,7 @@ public void process(final Postcard postcard, final InterceptorCallback callback)
if ("/test/activity4".equals(postcard.getPath())) {

// 这里的弹窗仅做举例,代码写法不具有可参考价值
final AlertDialog.Builder ab = new AlertDialog.Builder(MainActivity.getThis());
final AlertDialog.Builder ab = new AlertDialog.Builder(postcard.getContext());
ab.setCancelable(false);
ab.setTitle("温馨提醒");
ab.setMessage("想要跳转到Test4Activity么?(触发了\"/inter/test1\"拦截器,拦截了本次跳转)");
Expand Down Expand Up @@ -77,7 +75,6 @@ public void run() {
*/
@Override
public void init(Context context) {
mContext = context;
Log.e("testService", Test1Interceptor.class.getName() + " has init.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public final class Postcard extends RouteMeta {
private IProvider provider; // It will be set value, if this postcard was provider.
private boolean greenChannel;
private SerializationService serializationService;
private Context context; // May application or activity, check instance type before use it.
private String action;

// Animation
private Bundle optionsCompat; // The transition animation of activity
Expand Down Expand Up @@ -592,9 +594,6 @@ public String toString() {
super.toString();
}

//增加设置intent的action
private String action;

public String getAction() {
return action;
}
Expand All @@ -603,4 +602,12 @@ public Postcard withAction(String action) {
this.action = action;
return this;
}

public Context getContext() {
return context;
}

public void setContext(Context context) {
this.context = context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ protected <T> T navigation(Class<? extends T> service) {
return null;
}

// Set application to postcard.
postcard.setContext(mContext);

LogisticsCenter.completion(postcard);
return (T) postcard.getProvider();
} catch (NoRouteFoundException ex) {
Expand All @@ -281,6 +284,9 @@ protected Object navigation(final Context context, final Postcard postcard, fina
return null;
}

// Set context to postcard.
postcard.setContext(null == context ? mContext : context);

try {
LogisticsCenter.completion(postcard);
} catch (NoRouteFoundException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private void generateHelper() throws IOException, IllegalAccessException {
);
}

// Validater
// Validator
if (fieldConfig.required()) {
injectMethodBuilder.beginControlFlow("if (substitute." + fieldName + " == null)");
injectMethodBuilder.addStatement(
Expand Down

0 comments on commit 633526f

Please sign in to comment.