Detailed explanation of Android decoupling onActivityResult with Fragment skillfully

  • 2021-10-11 19:40:52
  • OfStack

Preface

I was working on jsbridge recently and wondered how it would be easier to do things like turn on the camera and take pictures in webView until I found this article

Two classes, so you don't have to implement onActivityResult () anymore

After reading it, I immediately imitated the code in this paper and realized it for 1 time.

Fragment:


public class OnActResultEventDispatcherFragment extends Fragment{
 public static final String TAG = "on_act_result_event_dispatcher";
 public int mRequestCode = 0x11;
 private SparseArray<ActResultRequest.Callback> mCallbacks = new SparseArray<>();

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setRetainInstance(true);
 }

 public void startForResult(Intent intent, ActResultRequest.Callback callback) {
  // mRequestCode And callback Need 11 Correspondence 
  mCallbacks.put(mRequestCode, callback);
  startActivityForResult(intent, mRequestCode);
  mRequestCode++;
 }

 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  ActResultRequest.Callback callback = mCallbacks.get(requestCode);
  mCallbacks.remove(requestCode);

  if (callback != null) {
   callback.onActivityResult(resultCode, data);
  }
 }
}

ActResultRequest:


public class ActResultRequest {
 private OnActResultEventDispatcherFragment fragment;

 public ActResultRequest(FragmentActivity activity) {
  fragment = getEventDispatchFragment(activity);
 }

 private OnActResultEventDispatcherFragment getEventDispatchFragment(FragmentActivity activity) {
  FragmentManager fragmentManager = activity.getSupportFragmentManager();


  OnActResultEventDispatcherFragment fragment = findEventDispatchFragment(fragmentManager);
  if (fragment == null) {
   fragment = new OnActResultEventDispatcherFragment();
   fragmentManager
     .beginTransaction()
     .add(fragment, OnActResultEventDispatcherFragment.TAG)
     .commitAllowingStateLoss();
   fragmentManager.executePendingTransactions();
  }
  return fragment;
 }

 private OnActResultEventDispatcherFragment findEventDispatchFragment(FragmentManager manager) {
  return (OnActResultEventDispatcherFragment) manager.findFragmentByTag(OnActResultEventDispatcherFragment.TAG);
 }

 public void startForResult(Intent intent, Callback callback) {
  fragment.startForResult(intent, callback);
 }

 public interface Callback {

  void onActivityResult(int resultCode, Intent data);
 }
}

MainActivity:


public class MainActivity extends AppCompatActivity {
 private static final String TAG = "MainActivity";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 
 public void start(View view) {
  Intent intent = new Intent(this, SecondActivity.class);
  ActResultRequest request = new ActResultRequest(this);
  request.startForResult(intent, new ActResultRequest.Callback() {
   @Override
   public void onActivityResult(int resultCode, Intent data) {
    Log.d(TAG, "resultCode = " + resultCode);
    String name = data.getStringExtra("name");
    Log.d(TAG, "name = " + name);
    Toast.makeText(MainActivity.this, "name = " + name + ", resultCode = " + resultCode,
      Toast.LENGTH_SHORT).show();
   }
  });
 }
}

SecondActivity


public class SecondActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_second);

 }

 public void returnResult(View view) {
  Intent intent = new Intent();
  intent.putExtra("name", "mundane");
  setResult(RESULT_OK, intent);
  finish();
 }
}

According to this train of thought, If you need to simplify the operation of startActivityForResult in jsbridge, We just need to bind Fragment and WebView to one. For example, name this Fragment WebViewFragment. There is only one WebView encapsulated by us in its layout. This WebView needs to listen to all the events of jsbridge. We write it in fragment, including the events in onActivityResult. Of course, we can also use a special management class to manage the registered events in this fragment. In the future, where WebView is useful in Activity, we will directly use this WebViewFragment. That is to say, take this WebViewFragment as our previous WebView.

github Address:

EasyOnActivityResult

Summarize


Related articles: