use FragmentManager class

The most appropriate way to handle Fragments in Android is to use FragmentManager class. Using fragments in Android programming is very useful advanced concept and benefits from it are:

  1. Dynamic and flexible user interface.
  2. Reusable views.
  3. Manage different screen resolutions.
  4. Changing appearance depending on orientation.
  5. Dividing activity into modules.
  6. Fragments allows Activity to use many fragments in an app. It means that fragments are flexible, while Activity is not.
  7. Fragments are in a way similar to div in web programming.

In previous tutorial, there is only one and with XML and behavior code very slim Activity. Next step is to add two fragments and to handle them from central fragment. So, add two empty fragments. Now, in CentralFragment declare button, Button btnFrone; for example. After declaration, let’s define this button. We must do this by using view object. So in onCreateView go with

public class CentralFragment extends Fragment {

Button btnFrone;

public CentralFragment() {

// Required empty public constructor

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.fragment_central, container, false);

btnFrone = (Button)view.findViewById(R.id.btnFr1);

return view;

}

}

DEclaration-of-the-Button-fro-the-View

For this button we create listener, and onClick(View view1) method:

btnFrone.setOnClickListener(new View.OnClickListener(){});

Implement-Method-onClick-for-btnFrone

btnFrone.setOnClickListener(new View.OnClickListener(){

public void onClick(View view1)

{

}

});

set-on-click-Listener-for-btnFrone

Inside onClick(View view1) method we finally use FragmentManager class.
1°Instance one object of FragmentManager class.
FragmentManager fragmentManager = getFragmentManager();

2°Instance one FragmentTransaction object connected to fragmentManager.
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

3°Instance one Fragment1 object
Fragment1 fragment1 = new Fragment1();

4°Add transaction:
fragmentTransaction.add(R.id.central_fragment,fragment1);

5°Add to back stack
fragmentTransaction.addToBackStack(null);

6°Commit
fragmentTransaction.commit();

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.fragment_central, container, false);

btnFrone = (Button)view.findViewById(R.id.btnFr1);

btnFrone.setOnClickListener(new View.OnClickListener(){

public void onClick(View view1)

{

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Fragment1 fragment1 = new Fragment1();

fragmentTransaction.add(R.id.central_fragment,fragment1);

fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

}

});

return view;

}

}

use-FragmentManager-class

Now, repeat the same procedure to show Fragment2. Whole code for showing fragment1 and fragment2 are:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// Inflate the layout for this fragment

View view = inflater.inflate(R.layout.fragment_central, container, false);

btnFrone = (Button)view.findViewById(R.id.btnFr1);

btnFtwo = (Button)view.findViewById(R.id.btnFr2);

btnFrone.setOnClickListener(new View.OnClickListener(){

public void onClick(View view1)

{

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Fragment1 fragment1 = new Fragment1();

fragmentTransaction.add(R.id.central_fragment,fragment1);

fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

}

});
btnFtwo.setOnClickListener(new View.OnClickListener(){

public void onClick(View view1)

{

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Fragment2 fragment2 = new Fragment2();

fragmentTransaction.add(R.id.central_fragment,fragment2);

fragmentTransaction.addToBackStack(null);

fragmentTransaction.commit();

}

});

return view;

}

Note: It is more practical to use image button then just button if we use fragments, since, with buttons, “Back” button click closes whole application, while with image button, it closes only current fragment.

Posted in android Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*