Android巧用Fragment解耦onActivityResult详解
前言
最近在研究jsbridge,一直在想怎么样才能让需要startActivityForResult比如打开相机拍照这样的操作在webView中能更简单一点,直到我发现了这篇文章
两个类,让你再也不用实现onActivityResult()
看了之后,马上模仿了文中的代码实现了一下.
Fragment:
publicclassOnActResultEventDispatcherFragmentextendsFragment{ publicstaticfinalStringTAG="on_act_result_event_dispatcher"; publicintmRequestCode=0x11; privateSparseArraymCallbacks=newSparseArray<>(); @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setRetainInstance(true); } publicvoidstartForResult(Intentintent,ActResultRequest.Callbackcallback){ //mRequestCode与callback需要一一对应 mCallbacks.put(mRequestCode,callback); startActivityForResult(intent,mRequestCode); mRequestCode++; } @Override publicvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ super.onActivityResult(requestCode,resultCode,data); ActResultRequest.Callbackcallback=mCallbacks.get(requestCode); mCallbacks.remove(requestCode); if(callback!=null){ callback.onActivityResult(resultCode,data); } } }
ActResultRequest:
publicclassActResultRequest{ privateOnActResultEventDispatcherFragmentfragment; publicActResultRequest(FragmentActivityactivity){ fragment=getEventDispatchFragment(activity); } privateOnActResultEventDispatcherFragmentgetEventDispatchFragment(FragmentActivityactivity){ FragmentManagerfragmentManager=activity.getSupportFragmentManager(); OnActResultEventDispatcherFragmentfragment=findEventDispatchFragment(fragmentManager); if(fragment==null){ fragment=newOnActResultEventDispatcherFragment(); fragmentManager .beginTransaction() .add(fragment,OnActResultEventDispatcherFragment.TAG) .commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); } returnfragment; } privateOnActResultEventDispatcherFragmentfindEventDispatchFragment(FragmentManagermanager){ return(OnActResultEventDispatcherFragment)manager.findFragmentByTag(OnActResultEventDispatcherFragment.TAG); } publicvoidstartForResult(Intentintent,Callbackcallback){ fragment.startForResult(intent,callback); } publicinterfaceCallback{ voidonActivityResult(intresultCode,Intentdata); } }
MainActivity:
publicclassMainActivityextendsAppCompatActivity{ privatestaticfinalStringTAG="MainActivity"; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } publicvoidstart(Viewview){ Intentintent=newIntent(this,SecondActivity.class); ActResultRequestrequest=newActResultRequest(this); request.startForResult(intent,newActResultRequest.Callback(){ @Override publicvoidonActivityResult(intresultCode,Intentdata){ Log.d(TAG,"resultCode="+resultCode); Stringname=data.getStringExtra("name"); Log.d(TAG,"name="+name); Toast.makeText(MainActivity.this,"name="+name+",resultCode="+resultCode, Toast.LENGTH_SHORT).show(); } }); } }
SecondActivity
publicclassSecondActivityextendsAppCompatActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } publicvoidreturnResult(Viewview){ Intentintent=newIntent(); 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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。