Activity与Fragment之间的传值问题
Activity传值给Fragment
添加Fragment的Activity:
Fragment01 fragment = new Fragment01();
Bundle bundle = new Bundle();
bundle.putString("str","这是Activity传来的值");
fragment.setArguments(bundle);
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fm01id,fragment01);
fragmentTransaction.commit();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Fragment中接受Activity传过来的值:
String str = (String)getArguments().get(“str”);
- 1
Fragment传值给Activity:
实现思路:
1.在Fragment中写一个回调接口
2.在activity中实现这个回调接口
3.在Fragment中onAttach 方法中得到activity中实现好的 实例化接口对象
4.用接口的对象 进行传值
Activity:
public class MainActivity extends Activity implements CallBackValue{
private TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.contents, new Fragmen1());
transaction.commit();
}
//要获取的值 就是这个参数的值
@Override
public void SendMessageValue(String strValue) {
// TODO Auto-generated method stub
tv1.setText(strValue);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
Fragment:
public class Fragmen1 extends Fragment{
private Button btn1;
private EditText et1;
CallBackValue callBackValue;
/**
* fragment与activity产生关联是 回调这个方法
*/
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
//当前fragment从activity重写了回调接口 得到接口的实例化对象
callBackValue =(CallBackValue) getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_layout1, container, false);
btn1 = (Button) view.findViewById(R.id.btn1);
et1 = (EditText) view.findViewById(R.id.et1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String strValue = et1.getText().toString().trim();
callBackValue.SendMessageValue(strValue);
}
});
return view;
}
//定义一个回调接口
public interface CallBackValue{
public void SendMessageValue(String strValue);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
onAttach的解决方案 :
今天在写一个Fragment的Demo中,在onAttach中进行赋值工作,结果显示并没有赋值,意味着onAttach并没有调用,最终在stackFlow找到了答案:
It’s not called because this method has been added in API 23. If you run your application
on a device with API 23 (marshmallow) then onAttach(Context)
will
be called. On all previous Android Versions onAttach(Activity)
will
be called.
我是看 onAttach(Activity) 方法被deprecation了,所以用最新的 onAttach(Context) 方法,哪想到在API低于 23 的版本中不会去调用后者,只会去调用onAttach(Activity)。下面是看到的一个比较好的解决方案:
- /*
- * onAttach(Context) is not called on pre API 23 versions of Android and onAttach(Activity) is deprecated
- * Use onAttachToContext instead
- */
- @TargetApi(23)
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- onAttachToContext(context);
- }
-
- /*
- * Deprecated on API 23
- * Use onAttachToContext instead
- */
- @SuppressWarnings(“deprecation”)
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
- onAttachToContext(activity);
- }
- }
-
- /*
- * Called when the fragment attaches to the context
- */
- protected void onAttachToContext(Context context) {
- //do something
- }
在用Fragment时,这样复写onAttach方法就可以减少API的影响了。ps:也真是出鬼了,昨天手机才升级的6.0,结果今天搞的手机测试正常,平板(api:4.4)测试不正常,愁死我了。这个问题告诉我,google不断发布兼容库意义重大啊!!!