巧用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巢狀
- Android開發中巧用Activity和FragmentAndroidFragment
- 架構師必備:巧用Canal實現非同步、解耦的架構架構非同步解耦
- 解耦解耦
- Android_Fragment_Fragment詳解AndroidFragment
- AppDelegate解耦APP解耦
- Fragment詳解Fragment
- iOS-模組解耦iOS解耦
- 手機天貓解耦之路解耦
- AntNest 模組完全解耦方案解耦
- 解耦 HTML、CSS 和 JavaScript解耦HTMLCSSJavaScript
- 解耦HTML、CSS和JavaScript解耦HTMLCSSJavaScript
- 物件導向與解耦物件解耦
- 解耦圖片載入庫解耦
- Spring 解耦反射建立物件Spring解耦反射物件
- 解耦設計手法總結解耦
- Android 碎片(Fragment)講解AndroidFragment
- 解耦解的早,改需求沒煩惱解耦
- 對前後端解耦的理解後端解耦
- 微服務解耦設計模式 - Neeraj微服務解耦設計模式
- 使用Materialise解耦微服務架構解耦微服務架構
- (更新,已反射hook到onActivityResult)如何避免使用onActivityResult,以提高程式碼可讀性反射Hook
- 呼叫startActivityForResult後onActivityResult立刻響應,返回當前頁onActivityResult不響應的問題
- 再見!onActivityResult!你好,Activity Results API!API
- 使用fragment載入自定義fragment出現error inflating class fragment錯誤解決辦法FragmentError
- [譯] 通過測試來解耦 Activity解耦
- Swift中依賴注入的解耦策略Swift依賴注入解耦
- MVC3使用Area解耦專案MVC解耦
- Android中Fragment巢狀Fragment,切換Fragment時不顯示檢視的原因及解決方法AndroidFragment巢狀
- Android之無法回撥onActivityResultAndroid
- Activity回傳資料方法startActivityForResult onActivityResult
- 用getChildFragmentManager解決Fragment巢狀Fragment資料丟失的問題Fragment巢狀
- ViewPager+Fragment LazyLoad 最優解ViewpagerFragment
- android Fragments詳解二:建立FragmentAndroidFragment
- android Fragments詳解四:管理fragmentAndroidFragment
- Spring:工廠模式哪裡解耦了?Spring模式解耦
- Android 通過 APT 解耦模組依賴AndroidAPT解耦