1、Activity <---> Fragment Activity一般先传递给FragmentActivity,通过FAct传给附属Fragment
2、Activity <---> FragmentActivity 通过Intent传递 如果涉及加载模式传递参数的话,需要通过 onNewIntent(Intent intent) 获取intent, getIntent()已经被onNewIntent()拦截
3、Fragment <---> Fragemt Fragment之间相互切换通过bundle,setArguments传递数据
4、FragmentActivity <---> Fragment 通过接口传递数据,因为Arguments在一开始创建Fragment就必须传进去,要是再次设置报
java.lang.IllegalStateException: Fragment already active 异常
12-19 10:28:01.393: E/AndroidRuntime(25224): java.lang.IllegalStateException: Fragment already active 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.support.v4.app.Fragment.setArguments(Fragment.java:530) 12-19 10:28:01.393: E/AndroidRuntime(25224): at com.pupuwang.sjzx.fragment.HomeFragment.setArguments(HomeFragment.java:198) 12-19 10:28:01.393: E/AndroidRuntime(25224): at com.pupuwang.sjzx.fragment.FragmentMainActivity.setFragmentIndicator(FragmentMainActivity.java:46) 12-19 10:28:01.393: E/AndroidRuntime(25224): at com.pupuwang.sjzx.fragment.FragmentMainActivity.onNewIntent(FragmentMainActivity.java:77) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1156) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2454) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.ActivityThread.performNewIntents(ActivityThread.java:2467) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2476) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.ActivityThread.access$1400(ActivityThread.java:157) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.os.Handler.dispatchMessage(Handler.java:99) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.os.Looper.loop(Looper.java:153) 12-19 10:28:01.393: E/AndroidRuntime(25224): at android.app.ActivityThread.main(ActivityThread.java:5326) 12-19 10:28:01.393: E/AndroidRuntime(25224): at java.lang.reflect.Method.invokeNative(Native Method) 12-19 10:28:01.393: E/AndroidRuntime(25224): at java.lang.reflect.Method.invoke(Method.java:511) 12-19 10:28:01.393: E/AndroidRuntime(25224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:848) 12-19 10:28:01.393: E/AndroidRuntime(25224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615) 12-19 10:28:01.393: E/AndroidRuntime(25224): at dalvik.system.NativeStart.main(Native Method)
fragment之间参数传递
public static HomeFragment newInstance(int index) { HomeFragment hf = new HomeFragment(); Bundle bundle = new Bundle(); bundle.putInt("index", index); hf.setArguments(bundle); Log.e(TAG, "--------index------:" +index); return hf; } public int getShownIndex() { Bundle bundle = getArguments(); if(bundle==null){ return -1; }else{ return bundle.getInt(IntentKeys.SHOP_INFO, 0); } }
接口回调传递参数
注意要在fragment中的此方法中转换下
@Override public void onAttachFragment(Fragment fragment) { try { if(fragment instanceof IBtnCallListener){ mbtnListener = (IBtnCallListener) fragment; }else if(fragment instanceof IFragmentArguments){ mIFragArg = (IFragmentArguments)fragment; } } catch (Exception e) { // TODO: handle exception } super.onAttachFragment(fragment); }
通过自定义接口回调
public interface IFragmentArguments { public void setIArguments(Bundle bundle); }
转载于:https://blog.51cto.com/lyp634959848/1343556