Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentTransaction inside a Fragment

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?

like image 909
TheLettuceMaster Avatar asked Mar 19 '13 15:03

TheLettuceMaster


People also ask

Can I have a fragment inside a fragment?

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() .

Which of the following is used to add fragment inside fragment?

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.

What is the use of fragmenttransaction?

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.

What is a transaction in fragmentmanager?

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.

What is an Android fragment?

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.

How to add a fragment inside another fragment?

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 :


1 Answers

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();
like image 156
waqaslam Avatar answered Sep 20 '22 23:09

waqaslam