Fragment data transmission and Fragment data transmission
Fragment is used in development more and more times, and many small projects are directly using Fragment as the carrier of Activity to switch pages. During development, the most important issue of page switching is data transmission. Today, we will focus on the Transmission Mode of switching data on the Fragment page.
Method 1: Bundle passing Parameters
Fragment1 fragment1 = new Fragment1 (); Bundle bundle = new Bundle (); bundle. putString ("name", "Michael"); fragment1.setArguments (bundle); FragmentManager fm = getFragmentManager (); FragmentTransaction transaction = fm. beginTransaction (); transaction. addToBackStack (null); transaction. add (R. id. content, fragment1); transaction. hide (HomeFragment. this); transaction. commit ();
Note that the existing Fragment must use Bundle to pass parameters.
It is relatively simple to accept the parameter. In half of the time, we will consider accepting the parameter in the onAttach lifecycle of Fragment.
The Code is as follows:
Bundle arguments = getArguments();
String name = arguments.getString("name");
Is it easy? Of course, we 'd better determine whether the obtained value is null, because data may not be obtained from the server when there is no network, and it will not be accepted here.
Next we will introduce the second parameter transfer method:
We will encounter such a demand in the marketplace client market. We also need to specify our shipping address and select services such as invoices when purchasing. In this way, we need to pass the selected results in another Fragment to the previous Fragment for relevant processing.
Normally, half of the Activity will be passed back by parameters.
@Override public void startActivityForResult(Intent intent, int requestCode) { // TODO Auto-generated method stub super.startActivityForResult(intent, requestCode); }
However, this is not recommended in Fragment. In Fragment, flexible callback interfaces are generally used to transmit parameters.
Therefore, we need to define an interface first. The interface code is as follows:
public interface OnResultListener { void OnResult(String value);}
The naming method here is more formal, imitating the Click Event in android.
Then we can perform such class operations:
1. Click one Fragment to go to another Fragmengt.
2. In another Fragment, a button is clicked to call back the parameter to the first Fragmennt.
In the second Fragment, we need to define a method. The passed parameter is an interface we defined earlier. this is used to assign values to this interface.
private OnResultListener listener; public void setListemer(OnResultListener listener) { this.listener = listener; }
Then, we can pass the value to the interface method parameters in this interface at the time.
@ Override public void onClick (View v) {switch (v. getId () {case R. id. f1_but: if (listener! = Null) {listener. onResult ("test");} else {Toast. makeText (getActivity (), "listener is null", Toast. LENGTH_SHORT ). show () ;}// the stack getFragmentManager () is displayed (). popBackStack (); break; default: break ;}}
The stack needs to be popped up here. When we click the OK button, the current Fragment will pop up and go to the first Fragment.
Because our first requirement is to go from the first Fragment to the second Fragment. So we also need to switch in the first Fragment.
FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.addToBackStack(null); transaction.add(R.id.content,fragment1); transaction.hide(HomeFragment.this); transaction.commit();
Note the following points:
1. Do not use replace for replacement. Use the add method. replace will first kill the view in Fragment and then the callback parameters will not be displayed.
2. Use add. Use Hide to avoid page stacking.
3. Pay attention to adding to the stack so that we can operate the return key, because Fragment is mounted to Acticity and does not add Fragment to the stack, clicking the return key directly performs operations on the current Activity.
If there is only one Activity, the current APP will exit directly.
I had to attach the source code, but I couldn't find where to upload the attachment. Sorry!