自己经常使用的知识点,每次到要用的时候都还要再查一次才能懂得使用,终于体会到总结的必要性了。
Activity传递数据给Fragment
Bundle bundle_fragment=new Bundle ();
String selecter=ClassList.get (position);
bundle_fragment.putString (ClassTag, selecter);
StudentFragment studentFragment = new StudentFragment ();//目标Fragment
studentFragment.setArguments (bundle_fragment);
//切换到学生列表
getFragmentManager ().beginTransaction ().replace (R.id.frameLayout_teacherMain, studentFragment).commit ()
Fragment传递给Activity
第一步
/**
*
* 这个接口用于判断当前是否请求到数据
*/
public interface JudgeRequstDataState {
public void RequstDataState(String JudgeState);
*
* 这个接口用于判断当前是否请求到数据
*/
public interface JudgeRequstDataState {
public void RequstDataState(String JudgeState);
}
//绑定父Activity
@Override
public void onAttach (Activity activity) {
super.onAttach (activity);
try {
//Activity实现接口在这里才能调用
RequstDataStateCallback = (TeacherMainActivity) activity;
}catch (ClassCastException e){
e.printStackTrace ();
}
}
第二步
public class TeacherMainActivity extends AppCompatActivity implements HomeFregment.JudgeRequstDataState//实现这个接口
//实现接口的方法
@Override
public void RequstDataState (String JudgeState) {
//根据传回来的数据进行处理
if (JudgeState.equals (HomeFregment.RequstSuccee))
{
Log.i ("TAG","RequstSuccee--------"+JudgeState);
pgbar_Flush.setVisibility (View.GONE);
}
{
Log.i ("TAG","RequstSuccee--------"+JudgeState);
pgbar_Flush.setVisibility (View.GONE);
}
}
Fragment之间传递数据跟Activity传值给Fragment一样,使用bundle,setArguments (bundle_fragment),就行