从片段A->片段B->片段C传输数据。当用户点击返回按钮时,我想回到片段A和数据[英] transfer data from fragment A -> fragment B - > fragment C. When user click Back button, I want to return to fragment A and data

本文是小编为大家收集整理的关于从片段A->片段B->片段C传输数据。当用户点击返回按钮时,我想回到片段A和数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我是Android开发人员的新手.我有一个有关3个片段转移的问题.

我有3个片段(a -b -c).我想要o从a-> b-> C传输数据.
在每个片段中,都会更改数据. 当用户单击返回按钮时,用户希望返回带有更新的数据. 如何使用更新数据返回片段A? 谢谢.

推荐答案

这是一个示例想法,如何实现交流.

 // activity classs
 public class SampleActivity extends Activity implements    OnFragmentChangeListener {
 OnBackPressListener  dataFragment;
 public void onCreate(bundle){

      android.app.FragmentManager fragmentManager=getFragmentManager();

    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    dataFragment = new DataFragment();
    fragmentTransaction.add(R.id.audio_permission_button,dataFragment);
    fragmentTransaction.commit()
 }
  @override
  public void OnFragmentChange(Bundle bundle){
  //here you go.
  // write code to load new fragment with same idea. now you have bundle do what you want.

}
@Override
public void onBackPressed() {
 // you can call this method from any click event, This just an sample idea.
 dataFragment.OnActivityBackPress();
}

}
 // interface to communicate with fragment
 public interface OnFragmentChangeListener {
     public void OnFragmentChange()
 }
 // fragment class
public class DataFragment extends Fragment implements OnBackPressListener {

 OnFragmentChangeListener onFragmentChangeListener;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 onFragmentChangeListener=(OnFragmentChangeListener) getActivity();


}

    @Override
public void OnActivityBackPress() {
 // pass you data to activity for loading new fragment or to refresh data.
 Bundle bundle= new Bundle();

 onFragmentChangeListener.OnFragmentChange(bundle);



}
}

// interface behave like mediator 
public interface OnBackPressListener {

public void OnActivtiyBackPress();
}

其他推荐答案

使用接口实现此目的.在片段和活动中实现界面,因为这是通过活动之间碎片交流的好方法.然后通过接口发送数据并从中提取数据.

其他推荐答案

  1. 您可以使用接口类在片段之间进行通信,但必须确保所有片段都活着.

  2. 您可以使用SharedPreferences,可以在其中保存数据并将其检索到您喜欢

  3. 的任何地方

本文地址:https://www.itbaoku.cn/post/2091056.html

问题描述

I'm a newbie in android developer. I have a question about transfer with 3 fragments.

I have 3 fragments (A - B - C). I want o transfer data from A -> B -> C.
In each the fragment, data was been changed. When user click BACK BUTTON, user want to return A with the updated data. How to return fragment A with the update data? Thanks.

推荐答案

Here is a sample idea how to achieve communication.

 // activity classs
 public class SampleActivity extends Activity implements    OnFragmentChangeListener {
 OnBackPressListener  dataFragment;
 public void onCreate(bundle){

      android.app.FragmentManager fragmentManager=getFragmentManager();

    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    dataFragment = new DataFragment();
    fragmentTransaction.add(R.id.audio_permission_button,dataFragment);
    fragmentTransaction.commit()
 }
  @override
  public void OnFragmentChange(Bundle bundle){
  //here you go.
  // write code to load new fragment with same idea. now you have bundle do what you want.

}
@Override
public void onBackPressed() {
 // you can call this method from any click event, This just an sample idea.
 dataFragment.OnActivityBackPress();
}

}
 // interface to communicate with fragment
 public interface OnFragmentChangeListener {
     public void OnFragmentChange()
 }
 // fragment class
public class DataFragment extends Fragment implements OnBackPressListener {

 OnFragmentChangeListener onFragmentChangeListener;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 onFragmentChangeListener=(OnFragmentChangeListener) getActivity();


}

    @Override
public void OnActivityBackPress() {
 // pass you data to activity for loading new fragment or to refresh data.
 Bundle bundle= new Bundle();

 onFragmentChangeListener.OnFragmentChange(bundle);



}
}

// interface behave like mediator 
public interface OnBackPressListener {

public void OnActivtiyBackPress();
}

其他推荐答案

Use interface to achieve this. Implements interface in fragment and activity, as it's a good way to communicate between fragment through activity. Then send the data through interface and extract the data from it.

其他推荐答案

  1. You can use Interface class to communicate between fragment but it must make sure all the fragment is alive.

  2. You can use SharedPreferences where you can save the data and retrieve it anywhere you like