Skip to content

Commit

Permalink
修复ActivityUtil中Intent跳转问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LiqiNew committed Sep 18, 2018
1 parent fa03a06 commit d507520
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ allprojects {
```
**2:依赖MyUtils**<br>
```gradle
compile 'com.github.liqinew:myutils:V.1.1.3'
compile 'com.github.liqinew:myutils:V.1.1.4'
```

### [点击查阅MyUtils-API文档](https://liqinew.github.io/MyUtils/)
Expand Down
84 changes: 84 additions & 0 deletions utils/src/main/java/com/liqi/utils/ActivityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public void startActivity(Context context, Class clazz) {
if (null != context && null != clazz) {
clearIntent();
mIntent.setClass(context, clazz);
if (!(context instanceof Activity)) {
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(mIntent);
} else {
Logger.e(TAG + "startActivity()", "参数值传入为空");
Expand Down Expand Up @@ -139,6 +142,67 @@ public void startActivityClearAllInstances(Context context, Class<?> clazz) {
// }
// }

/**
* 获取配置完毕的intent对象
*
* @param context 上下文
* @param clazz 目标activity.class
* @param data Object类型的数据
* key:名称
* value:Object类型值,支持的类型(String,Integer,Boolean,Double,Float,Long,Serializable)
*/
public Intent getConfigurationIntent(Context context, Class clazz, Map<String, Object> data) {
if (null != context && null != clazz) {
clearIntent();
mIntent.setClass(context, clazz);
if (data != null) {
for (Entry<String, Object> en : data.entrySet()) {
Object value = en.getValue();
if (value instanceof String) {
mIntent.putExtra(en.getKey(), en.getValue().toString());
continue;
}
if (value instanceof Integer) {
mIntent.putExtra(en.getKey(), (int) en.getValue());
continue;
}
if (value instanceof Boolean) {
mIntent.putExtra(en.getKey(), (boolean) en.getValue());
continue;
}
if (value instanceof Double) {
mIntent.putExtra(en.getKey(), (double) en.getValue());
continue;
}
if (value instanceof Float) {
mIntent.putExtra(en.getKey(), (float) en.getValue());
continue;
}
if (value instanceof Long) {
mIntent.putExtra(en.getKey(), (long) en.getValue());
continue;
}
if (value instanceof Serializable) {
mIntent.putExtra(en.getKey(), (Serializable) en.getValue());
} else {
mIntent.putExtra(en.getKey(), "");
Logger.e(TAG + "getConfigurationIntent()", "传入的类型不符合规定>>>>默认传输值:\"\"");
}

}
} else {
Logger.e(TAG + "getConfigurationIntent()", "参数data的值传入为空");
}

if (!(context instanceof Activity)) {
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
} else {
Logger.e(TAG + "getConfigurationIntent()", "参数值传入为空");
}
return mIntent;
}

/**
* 开启一个新的activity
*
Expand Down Expand Up @@ -190,6 +254,11 @@ public void startActivityWithObjectData(Context context, Class clazz, Map<String
} else {
Logger.e(TAG + "startActivityWithObjectData()", "参数data的值传入为空");
}

if (!(context instanceof Activity)) {
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

context.startActivity(mIntent);
} else {
Logger.e(TAG + "startActivityWithObjectData()", "参数值传入为空");
Expand Down Expand Up @@ -452,6 +521,10 @@ public void startActivityForData(Context context, Class clazz, Serializable seri
Logger.e(TAG + "startActivityForData()", "参数datas的值传入为空");
}

if (!(context instanceof Activity)) {
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

context.startActivity(mIntent);
} else {
Logger.e(TAG + "startActivityForData()", "参数值传入为空");
Expand Down Expand Up @@ -502,6 +575,11 @@ public void startActivityForBundleListObj(Context context, Class clazz, List<Ser
} else {
Logger.e(TAG + "startActivityForBundleListObj()", "参数list的值传入为空");
}

if (!(context instanceof Activity)) {
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

context.startActivity(mIntent);
} else {
Logger.e(TAG + "startActivityForBundleListObj()", "参数值传入为空");
Expand All @@ -527,6 +605,11 @@ public void startActivityForObj(Context context, Class clazz, Serializable seria
} else {
Logger.e(TAG + "startActivityForObj()", "参数serializableObj的值传入为空");
}

if (!(context instanceof Activity)) {
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

context.startActivity(mIntent);
} else {
Logger.e(TAG + "startActivityForObj()", "参数值传入为空");
Expand Down Expand Up @@ -588,6 +671,7 @@ public void startActivityForObjWhy(Activity activity, Class clazz, Serializable
* 清空掉intent里面存储的数据
*/
private void clearIntent() {
mIntent.setFlags(0);
Bundle extras = mIntent.getExtras();
if (null != extras && !extras.isEmpty())
extras.clear();
Expand Down

0 comments on commit d507520

Please sign in to comment.