问题描述
我是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(); }
其他推荐答案
使用接口实现此目的.在片段和活动中实现界面,因为这是通过活动之间碎片交流的好方法.然后通过接口发送数据并从中提取数据.
其他推荐答案
-
您可以使用接口类在片段之间进行通信,但必须确保所有片段都活着.
-
您可以使用SharedPreferences,可以在其中保存数据并将其检索到您喜欢
的任何地方
问题描述
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.
其他推荐答案
You can use Interface class to communicate between fragment but it must make sure all the fragment is alive.
You can use SharedPreferences where you can save the data and retrieve it anywhere you like