Interaction between Fragment and Activity in Android (two implementation methods), androidfragment
(BackGound is not set for Fragment layout)
Previously, I wrote a blog post titled two methods for creating Fragment in Android. I have made a detailed analysis on how to create a Fragment hybrid layout. Today I will explain in detail how Fragment interacts with the host Activity.
We can understand that to achieve information interaction between Fragment in the host Activity, it is impossible to directly implement information interaction between Fragment through the host Activity.
Fragment and Fragment or data interaction with Activity (deploy the layout contained in some Fragment in Activity) I personally summarize two methods:
(1) passing parameters through Bundle to achieve data interaction between Fragment
(2) define a callback interface in Fragment and implement it by the host Activity. When the Activity receives a callback through the interface, it can share information with other Fagment in the layout when necessary.
Before starting the two implementation methods, I would like to introduce and demonstrate the effects of some of the personalized parameters after different settings. I have previously introduced you to manage Fragment in the host Activity, fragmentManager (FragmentManager) must be obtained, while FragmentManager must enable FragmentTransaction to add, delete, change, and other operations (transactions) to Fragment. Here we will show you how to addFragmentTransaction. addToBackStackBefore and afterNo BackGround is set for FragmentIn this case, the different effects of FragmentTransaction. add and replace are used. For more details about the method and usage, refer to the API for details.
Not Set
FragmentTransaction. addToBackStackMethod demonstration effect (after opening multiple layers, press back to directly exit the program ):
Set FragmentTransaction. addToBackStackMethod demonstration effect (no listener for monitoring background stack changes is set here for judgment and processing ):
It seems that there is no difference. In fact, when you press the rollback key, this is to exit one by one based on the order of opening.
No BackGround is set for FragmentUse FragmentTransaction. the demo Effect of add: pay attention to it. Here we emphasize that BackGound is not set for the Fragment layout on the right. If BackGound is set, the implementation effect is no different from replace, this is also the main reason why so many times out today.
We can see different implementation effects. We started to demonstrate the implementation code:
We are using the layout file in the two methods of Fragment creation in Android (adding fragment to the host Activity through java code), layout File Code refer to http://www.cnblogs.com/panhouye/p/6185093.html
(1) passing parameters through Bundle
Step 1:
RightFragment. java:
1 import android. app. fragment; 2 import android. OS. bundle; 3 import android. view. layoutInflater; 4 import android. view. view; 5 import android. view. viewGroup; 6 import android. widget. textView; 7/** 8 * Created by panchengjia on 2016/12/18. 9 */10 public class RightFragment extends Fragment {11 public RightFragment () {12} 13/* Fragment parameter passing method (passed through the Bundle object) 14 * using this parameter passing method ensures that the 15 * parameters passed by the user During Horizontal/vertical screen switching do not lose 16 */17 public static RightFragment getInstance (String data) {18 RightFragment rightFragment = new RightFragment (); 19 Bundle bundle = new Bundle (); 20 // pass the string to be passed in the form of a key-value pair to bundle21 bundle. putString ("data", data); 22 rightFragment. setArguments (bundle); 23 return rightFragment; 24} 25 @ Override26 public void onCreate (Bundle savedInstanceState) {27 super. onCreate (savedInstanceState); 28} 29 @ Override30 public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {31 View view = inflater. inflate (R. layout. right_layout, container, false); 32 TextView TV = (TextView) view. findViewById (R. id. TV); 33 String data = getArguments (). getString ("data"); 34 TV. setText (data); 35 return view; 36} 37 @ Override38 public void onPause () {39 super. onPause (); 40} 41}
Step 2:
The java implementation code MainActivity. java corresponding to the active layout host Activity:
1 import android. app. fragmentManager; 2 import android. app. fragmentTransaction; 3 import android. support. v7.app. appCompatActivity; 4 import android. OS. bundle; 5 import android. view. view; 6 import android. widget. button; 7 public class Main3Activity extends AppCompatActivity {8 FragmentManager fragmentManager; 9 FragmentTransaction fragmentTransaction; 10 LeftFragment leftFragment; 11 Button panhouye, bikonghai; // declare the Button 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_main2); 16 // get fragmentManager17 fragmentManager = getFragmentManager (); 18 // use findFragmentById to find leftFragment19 leftFragment = (LeftFragment) fragmentManager. findFragmentById (R. id. left); 20 // find the corresponding navigation Button and set to click event 21 panhouye = (Button) leftFragment. getView (). findViewById (R. id. panhouye); 22 bikonghai = (Button) leftFragment. getView (). findViewById (R. id. bikonghai); 23 panhouye. setOnClickListener (new View. onClickListener () {24 @ Override25 public void onClick (View v) {26 // call the method to modify the text content in rightfragment 27 switchButton ("I am Pan Hou ye "); 28} 29}); 30 bikonghai. setOnClickListener (new View. onClickListener () {31 @ Override32 public void onClick (View v) {33 switchButton (""); 34} 35 }); 36 // set the default text content 37 switchButton ("I am Pan Hou ye") in rightfragment after opening the Activity; 38} 39 // define the method to fill in fragment on the right of the Activity, modify the text content by passing the parameter 40 public void switchButton (String data) {41 fragmentManager = getFragmentManager (); 42 fragmentTransaction = fragmentManager. beginTransaction (); 43 // call the getInstance method in RightFragment to modify the text 44 RightFragment rightFragment = RightFragment. getInstance (data); 45 // when the add method is used, the text in the right fragment overlaps (when the BackGround is not set) 46 fragmentTransaction. replace (R. id. right, rightFragment); 47 fragmentTransaction. commit (); 48} 49}
(2) interface callback Step 1:
LeftFragment. java file of the left-side fragment java Implementation Code
In this demonstration, click the button in the left Fragment to trigger data interaction with the right Fragment. Therefore, you need to add a callback interface in this class to call back and modify the method of the right text in the host Activity.
1 import android. app. fragment; 2 import android. OS. bundle; 3 import android. view. layoutInflater; 4 import android. view. view; 5 import android. view. viewGroup; 6 import android. widget. button; 7/** 8 * Created by panchengjia on 2016/12/18. 9 */10 public class LeftFragment extends Fragment implements View. onClickListener {11 // declare the internally defined callback interface 12 CallBackListener callBackListener; 13 // declare the event trigger buttons in the layout 14 buttons panhouye, bikonghai; 15 @ Override16 public void onCreate (Bundle savedInstanceState) {17 super. onCreate (savedInstanceState); 18 // use getActivity () to obtain the interface 19 callBackListener = (CallBackListener) getActivity () for callback to Modify text methods (); 20} 21 @ Override22 public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {23 View view = inflater. inflate (R. layout. left_layout, container, false); 24 panhouye = (Button) view. findViewById (R. id. panhouye); 25 bikonghai = (Button) view. findViewById (R. id. bikonghai); 26 panhouye. setOnClickListener (this); // set the listener event for the button 27 bikonghai. setOnClickListener (this); 28 return view; 29} 30 @ Override31 public void onPause () {32 super. onPause (); 33} 34 35 @ Override36 public void onClick (View v) {37 switch (v. getId () {38 case R. id. panhouye: 39 callBackListener. setText ("I am Pan Hou ye"); 40 break; 41 case R. id. bikonghai: 42 callBackListener. setText ("I Am a bikong"); 43 break; 44} 45} 46 // set the callback interface for modifying text 47 public static interface CallBackListener {48 public void setText (String data); 49} 50}
Step 2:
RightFragment. java file of the right fragment java Implementation Code
In this demonstration, fragment is displayed on the right side of the text. After clicking the button on the left side, the click event is processed by changing the text. Therefore, you must add the text modification method to the Fragment class.
1 import android. app. fragment; 2 import android. OS. bundle; 3 import android. support. annotation. nullable; 4 import android. view. layoutInflater; 5 import android. view. view; 6 import android. view. viewGroup; 7 import android. widget. textView; 8/** 9 * Created by panchengjia on 2016/12/18. 10 */11 public class RightFragment extends Fragment {12 // declare TextView in fragment to create a method to Modify text 13 private TextView TV; 14 @ Override15 public void onCreate (Bundle savedInstanceState) {16 super. onCreate (savedInstanceState); 17} 18 @ Override19 public void onPause () {20 super. onPause (); 21} 22 @ Nullable23 @ Override24 public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {25 View view = inflater. inflate (R. layout. right_layout, container, false); 26 TV = (TextView) view. findViewById (R. id. TV); 27 return view; 28} 29 // here you can set the method to modify your own text. 30 public void setFragmentText (String data) {31 TV. setText (data); 32} 33}
Step 3:
The java implementation code MainActivity. java file of the host Acvtivity on the main interface (in order to receive the Fragment Event Callback, the host Activity must implement the callback Interface ):
1 import android. app. fragmentManager; 2 import android. app. fragmentTransaction; 3 import android. support. v7.app. appCompatActivity; 4 import android. OS. bundle; 5 public class MainActivity extends AppCompatActivity implements LeftFragment. callBackListener {6 FragmentManager fragmentManager; 7 FragmentTransaction fragmentTransaction; 8/* leftfragment has been declared in the main layout file. 9 * Here you only need to deploy the worker */11 RightFragment rightFragment; 12 @ Override13 protected void onCreate (Bundle savedInstanceState) {14 super. onCreate (savedInstanceState); 15 setContentView (R. layout. activity_main); 16 // initialize the main layout (mainly to fill fragments with the main layout) 17 initActivity (); 18} 19 private void initActivity () {20 fragmentManager = getFragmentManager (); 21 fragmentTransaction = fragmentManager. beginTransaction (); 22 rightFragment = new RightFragment (); 23 fragmentTransaction. add (R. id. right, rightFragment); 24 fragmentTransaction. commit (); 25} 26 // interface implementation method, used to call back the method of modifying text defined in the RightFragment class 27 @ Override28 public void setText (String data) {29 rightFragment. setFragmentText (data); 30} 31}
Summary:
The implementation method of interface callback in this demonstration seems to be a little larger than the code for passing parameters by bundle. However, in actual development, the Fragment we face is not just the current two, the method of implementing interactive data through interface callback can better reuse the Fragment UI component and fundamentally solve a large number of code reuse problems. We recommend that you master interface callback to implement data interaction.