Tired of writing onActivityResult? Don’t worry, the transparent Fragment will help you
Although fragment can kill you, it is still useful to use it as an auxiliary function without writing a view.
RxPermissionsFragment: github.com/hss01248/St…
The base class
public BaseTransFragment(FragmentActivity activity, Bean bean) {
this.activity = activity;
this.bean = bean;
startFragmentTransaction();
}
private void startFragmentTransaction() {
try {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(this, UUID.randomUUID().toString())
.commitNowAllowingStateLoss();
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if(debugable){
Log.d("frag"."onCreate:"+this);
}
}
public void finish(){ try { getActivity().getSupportFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss(); }catch (Throwable throwable){ throwable.printStackTrace(); }}Copy the code
Example:
OnactivityResult processing:
class InAppResultFragment extends BaseTransFragment<Intent> {
public InAppResultFragment(FragmentActivity activity, Intent intent) {
super(activity, intent);
}
TheActivityListener listener;
public void startActivityForResult(TheActivityListener listener){
try {
this.listener = listener;
startActivityForResult(bean,new Random().nextInt(589));
}catch (Throwable e){
listener.onActivityNotFound(e);
if(StartActivityUtil.debugable){
e.printStackTrace();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (StartActivityUtil.debugable) {
Log.i("onActivityResult"."req:" + requestCode + ",result:" + resultCode + ",data:" + data);
}
listener.onActivityResult(requestCode,resultCode,data);
finish();
}
}
使用:
new InAppResultFragment(activity,intent).startActivityForResult(listener);
Copy the code
Here’s a utility class that starts an activity to get the result:
Start the in-app page and you get the onActivityCreated and onActivityResult callbacks where onActivityCreated is implemented through the registration lifecycle on the application
StartActivityUtil.startActivity(this, ActivityDemo2.class, null,true, new TheActivityListener<ActivityDemo2>() { @Override public void onActivityCreated(@NonNull ActivityDemo2 activity, @nullable Bundle savedInstanceState) {// Set the data here and take it directly after the super.oncreate () of the specific activityOnCreate() method activityOnCreate(); Toast.makeText(activity,"Activity onCreate callback", Toast.LENGTH_LONG).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(MainActivity.this, "Activity onActivityResult callback", Toast.LENGTH_LONG).show(); }});Copy the code
Start the third party app and receive/test results on return:
StartActivityUtil.goOutAppForResult(this, intent, new OutActivityResultListener() {
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
boolean hasPermission = NotificationManagerCompat.from(MainActivity.this).areNotificationsEnabled();
Toast.makeText(MainActivity.this,"Notification bar permission :"+hasPermission,Toast.LENGTH_SHORT).show();
}
@Override
public void onActivityNotFound(Throwable e) {
}
});
Copy the code
Example 2. Photo/image select-Clip-Compress the entire process, encapsulated into a tool method:
TakePhotoUtil.startPickOne(this, false, new TakeOnePhotoListener() {
@Override
public void onSuccess(String s) {
showImg(filePath);
}
@Override
public void onFail(String path, String msg) {
MyToast.errorBigL(s1);
}
@Override
public void onCancel() {}});Copy the code