Android巧用Fragment解耦onActivityResult详解

发布时间:2020-08-27 15:27:32 作者:mundane
来源:脚本之家 阅读:164

前言

最近在研究jsbridge, 一直在想怎么样才能让需要startActivityForResult比如打开相机拍照这样的操作在webView中能更简单一点, 直到我发现了这篇文章

两个类,让你再也不用实现onActivityResult()

看了之后, 马上模仿了文中的代码实现了一下.

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与callback需要一一对应
  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();
 }
}

按照这样的思路, 如果需要简化jsbridge中startActivityForResult的操作, 我们只需要把Fragment和WebView绑定在一起.比如给这个Fragment取名叫WebViewFragment, 它的布局中只有一个我们封装好的WebView, 这个WebView需要监听jsbridge的所有事件我们都在fragment中写好, 包括onActivityResult中的事件, 当然也可以在这个fragment中用一个专门的管理类来管理注册的事件.以后Activity中有用到WebView的地方我们就直接用这个WebViewFragment就好了.也就是说, 把这个WebViewFragment, 当成我们之前的WebView.

github地址:

EasyOnActivityResult

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。

推荐阅读:
  1. Fragment使用详解
  2. Android Fragment

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

fragment 解耦 onactivityresult

上一篇:CSS中实现首行缩进效果的方法有哪些

下一篇:HTML中onclick属性实现单击处理的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》