巧用Fragment解耦onActivityResult
最近在研究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地址:
相關文章
- 巧用 LLVM 特性: Objective-C Class Properties 解耦LVMObject解耦
- 【Android Fragment】解決Fragment多層巢狀時onActivityResult無法正確回撥的問題AndroidFragment巢狀
- 架構師必備:巧用Canal實現非同步、解耦的架構架構非同步解耦
- 解耦解耦
- AppDelegate解耦APP解耦
- Android 碎片(Fragment)講解AndroidFragment
- iOS-模組解耦iOS解耦
- Spring 解耦反射建立物件Spring解耦反射物件
- 使用fragment載入自定義fragment出現error inflating class fragment錯誤解決辦法FragmentError
- 解耦圖片載入庫解耦
- 手機天貓解耦之路解耦
- AntNest 模組完全解耦方案解耦
- Android中Fragment巢狀Fragment,切換Fragment時不顯示檢視的原因及解決方法AndroidFragment巢狀
- 對前後端解耦的理解後端解耦
- 微服務解耦設計模式 - Neeraj微服務解耦設計模式
- 使用Materialise解耦微服務架構解耦微服務架構
- Android之無法回撥onActivityResultAndroid
- 再見!onActivityResult!你好,Activity Results API!API
- FragmentFragment
- Spring:工廠模式哪裡解耦了?Spring模式解耦
- Swift中依賴注入的解耦策略Swift依賴注入解耦
- [譯] 通過測試來解耦 Activity解耦
- 詳解command設計模式,解耦操作和回滾設計模式解耦
- 【Android Fragment】友盟統計 Fragment 頁面顯示隱藏的完美解決方案AndroidFragment
- Android Fragment用法知識點的講解AndroidFragment
- Fragment新功能,setMaxLifecycle瞭解一下Fragment
- Android T-MVVM深度解耦開發框架AndroidMVVM解耦框架
- Android 通過 APT 解耦模組依賴AndroidAPT解耦
- AndroidLifecycle對MVP模式進一步”解耦“AndroidMVP模式解耦
- Java 工程解耦即高內聚低耦合Java解耦
- 如何優雅的對UITableView進行解耦UIView解耦
- 【Android Fragment】解決ViewPager巢狀時Fragment的mUserVisibleHint屬性不同步的問題AndroidFragmentViewpager巢狀
- 探索 單個Fragment實現沉浸式,其餘Fragment不實現的解決辦法Fragment
- 巧用ReduxRedux
- 29.Flutter與原生解耦式混合開發Flutter解耦
- 命令模式-接收者與執行者解耦和模式解耦
- 2020-09-24: 什麼是應用解耦?解耦
- [譯] 用依賴注入解耦你的程式碼依賴注入解耦