内容简介:看了之后, 马上模仿了文中的代码实现了一下.Fragment:ActResultRequest:
最近在研究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.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
现代应用数学手册
《现代应用数学手册》编委会 / 清华大学出版社 / 2005-1-1 / 48.00元
本书是进行科学计算的常备工具书,内容新颖,查阅方便,实用性强。主要介绍生产、科研、管理、数学等实践中在计算机上使用的各种计算方法和技巧。全书分为14章,依次为数值计算概论、插值法、函数逼近与曲线拟合、数值积分与数值微分、方程求根、线性方程组的直接解法和迭代解法、矩阵特征值问题、非线性方程组数值解与最优化方法、常微分方程初值问题和边值问题的数值解法、偏微分方程的数值解法、多重网络法和积分方程数值解法......一起来看看 《现代应用数学手册》 这本书的介绍吧!