Skip to content

Commit

Permalink
增加onInterceptStartIntent回调
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Feb 23, 2022
1 parent 05f1949 commit f49ec22
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,48 @@ start a activity and set data in it's onCreate listener, rather then send data b

and get result in listener, not in activity's onActivityResult callback

## 接口定义:

```java
public interface ActivityResultListener {

/**
*
* @param fragment
* @param intent
* @return 返回值代表是否拦截原默认行为. 如果
* 调用处为: if(!listener.onInterceptStartIntent(this,bean)){
* startActivityForResult(bean,requestCode);
* }
*/
default boolean onInterceptStartIntent(@NonNull Fragment fragment, @Nullable Intent intent, int requestCode){
return false;
}

/**
* 在里面自己处理
* @param requestCode
* @param resultCode
* @param data
*/
void onActivityResult(int requestCode, int resultCode, @Nullable Intent data);





void onActivityNotFound(Throwable e) ;
}
```









```
StartActivityUtil.startActivity(this, ActivityDemo2.class, null,true,
new TheActivityListener<ActivityDemo2>() {
Expand Down
4 changes: 4 additions & 0 deletions activityresult/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
* time:2020/4/30
Expand All @@ -13,6 +15,19 @@
*/
public interface ActivityResultListener {

/**
*
* @param fragment
* @param intent
* @return 返回值代表是否拦截原默认行为. 如果
* 调用处为: if(!listener.onInterceptStartIntent(this,bean)){
* startActivityForResult(bean,requestCode);
* }
*/
default boolean onInterceptStartIntent(@NonNull Fragment fragment, @Nullable Intent intent, int requestCode){
return false;
}

/**
* 在里面自己处理
* @param requestCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public void goOutApp(ActivityResultListener listener){
requestCode = new Random().nextInt(8799);
this.listener = listener;
try {
startActivityForResult(bean,requestCode);
if(!listener.onInterceptStartIntent(this,bean,requestCode)){
startActivityForResult(bean,requestCode);
}
}catch (Throwable throwable){
listener.onActivityNotFound(throwable);
finish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ public InAppResultFragment(FragmentActivity activity, Intent intent) {
super(activity, intent);
}
ActivityResultListener listener;
int requestCode;
public void startActivityForResult(ActivityResultListener listener){
try {
this.listener = listener;
startActivityForResult(bean,new Random().nextInt(589));
requestCode = new Random().nextInt(589);
if(!listener.onInterceptStartIntent(this,bean,requestCode)){
startActivityForResult(bean,requestCode);
}

}catch (Throwable e){
listener.onActivityNotFound(e);
if(StartActivityUtil.debugable){
Expand All @@ -40,6 +45,15 @@ public void onActivityResult(int requestCode, int resultCode, @Nullable Intent d
}
listener.onActivityResult(requestCode,resultCode,data);
finish();
if(requestCode == this.requestCode){
//listener.onActivityResult(requestCode,resultCode,data);
//finish();
}else {
if (StartActivityUtil.debugable) {
Log.w("onActivityResult", "reqcode not same:" + requestCode + ",this.requestCode:" + this.requestCode );
}
}


}
}

0 comments on commit f49ec22

Please sign in to comment.