I am using jfeinstien10's SlidingMenu
. The menu that pulls out is a Fragment
. When the user clicks on a menu item, it does something like this:
FragmentTransaction t = getActivity().getSupportFragmentManager().beginTransaction();
SherlockListFragment mFrag = new ItemsFragment();
t.replace(R.id.main_frag, mFrag);
t.commit();
However, I have heard that it is not good practice to control a Fragment
from Another Fragment
. In a situation like this, is this an acceptable method? Or should I be using a Callback Method to the parent FragmentActivity
.
If a Callback method is the proper way, can I please see an example of how this works?
Fragments are also capable of hosting one or more child fragments. Inside a fragment, you can get a reference to the FragmentManager that manages the fragment's children through getChildFragmentManager() . If you need to access its host FragmentManager , you can use getParentFragmentManager() .
There are two ways to add a fragment to an activity: dynamically using Java and statically using XML. Before embedding a "support" fragment in an Activity make sure the Activity is changed to extend from FragmentActivity or AppCompatActivity which adds support for the fragment manager to all Android versions.
FragmentTransaction: The class for performing an atomic set of Fragment operations such as Replace or Add a Fragment. Below is the example of Fragment’s. In this example we create two Fragments and load them on the click of Button ’s. We display two Button’s and a FrameLayout in our Activity and perform setOnClickListener event on both Button’s.
At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.
An Android fragment is a GUI component which can "live" inside an Activity. An Android fragment is not by itself a subclass of View which most other GUI components are. Instead, a fragment has a view inside it. It is this view which is eventually displayed inside the activity in which the fragment lives.
Fragments can be added inside other fragments but then you will need to remove it from parent Fragment each time when onDestroyView () method of parent fragment is called. And again add it in Parent Fragment's onCreateView () method. Just do like this :
Instead of performing transaction in fragment, I would suggest you to leave it up to Activity level. To do that, define a public method in your activity and then call it from your fragment. For example:
Suppose your activity is:
class MainActivity extends Activity{
....
public void replaceFragment(){
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
SherlockListFragment mFrag = new ItemsFragment();
t.replace(R.id.main_frag, mFrag);
t.commit();
}
....
}
And within your fragment, make the following call:
((MainActivity)getActivity()).replaceFragment();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With