我有两项活动,分别是活动A和活动B.
活动A包含碎片AF1,AF2。 活动B包含碎片BF1,BF2。
目前我在AF1。
答案 0 :(得分:0)
第一种方法是使用接口。以下是步骤的细分:
本教程中显示了上述过程:how to communicate between fragments and activities
<强> FragmentOne.java 强>
public class FragmentOne extends Fragment{
private Activity activity;
@Override
public void onAttach(Activity act){
super.onAttach(act);
activity = act;
}
@Override
public View onCreateView(..........){
/* somethind was clicked here*/
try{
((OnSomethingClickedListener) activity).updateActivity(position);
}catch(ClassCastException e){}
return view;
}
public interface OnSomethingCllickedListener{
void updateActivity(int position);
}
}
在您的活动中,实施界面并覆盖上述方法:
public class ActivityOne extends Activity implements FragmentOne.OnSomethingCllickedListener{
@Override
public void onCreate(Bundle saveInstanceState){
/* as usual here */
}
@Override
public void updateActivity(int position){
/* call FragmentTwo's method here to update the view based on the position of item clicked here*/
FragmentTwo.updateView(position);
}
}
其次,要将您的活动与片段分离,请使用EventBus库。这很简单,就像这样:
我希望这会有所帮助。
答案 1 :(得分:-1)
Bundle bundle =new Bundle();
bundle.putString("message", message);
Frag1 _fragment = new Frag1();
fragmentTransaction.replace(android.R.id.content, _fragment);
fragmentTransaction.commit();
Bundle bundle=getArguments(); //get data
if(bundle!=null)
{
message=bundle.getString("message") ;
}