Skip to content Skip to sidebar Skip to footer

Calling A Fragment From Fragment

I want to call another fragment from the current fragment on the click of the button in the current fragment. Here is my Mainactivity : import android.app.FragmentManager; import

Solution 1:

For doing a fragment transaction.Please do the following.

Eg.. A and B are fragments. Currently A is visible to the User. If you want to do a transaction. Just create as like below

Bb=newB(); 
((YourActivity)getActivity).setnewFragment(b,true);

publicvoidsetNewFragment(Fragment fragment,boolean addbackstack) {
        FragmentManagermanager= getSupportFragmentManager();
        FragmentTransactiontransaction= manager.beginTransaction();
        transaction.replace(R.id.fragment_content, fragment);
        if (addbackstack)
            transaction.addToBackStack(title);
        transaction.commit();
    }

Solution 2:

Use an interface to communicate between fragments. For example

public YourFirstFragment extends Fragment {

    publicinterfaceFragmentCallBack {
        voidcallBack();
    }

    private FragmentCallBack fragmentCallBack;

    publicvoidsetFragmentCallBack(FragmentCallBack fragmentCallBack) {
        this.fragmentCallBack = fragmentCallBack;
    }

    privatevoidcallThisWhenYouWantToCallTheOtherFragment(){
        fragmentCallBack.callBack();
    }
}

Then in You activity

privatevoid setCallToFragment{
  yourFirstFragment.setFragmentCallBack(new FragmentCallBack(){
   voidcallBack(){
    yourSecondFragment.doWhatEver();
  }})

}

Solution 3:

First you need to create an interface which defines methods that will be used by your fragment to invoke code from the respective activity it is attached to

publicinterfaceOnFragmentInteractionlistener {
    // the method that you call from your fragment, // add whatever parameter you need in the implementation of this// method.voidonClickOfMyView();
}

once you have created the interface, implement it any and all activities that use this fragment.

publicclassMainActivityextendsAppCompatActivityimplementsOnMyFragmentInteractionListener {

    @OverridepublicvoidonClickOfMyView() {
        // DO your on click logic here, like starting the transaction// to add/replace another fragment.
    }
}

Then in the onAttach of your fragment to an activity be sure to check if the attaching activity implements this interface, else throw a RunTimeException like so

@OverridepublicvoidonAttach(Context context) {
    super.onAttach(context);
    if (context instanceofOnMyFragmentInteractionListener) {
        mListener = (OnMyFragmentInteractionListener) context;
    } else {
        thrownewRuntimeException(context.toString()
                + " must implement OnMyFragmentInteractionListener");
    }
}

Here mListener is a interface reference you hold in your fragment class through which you invoke the onClickOfMyView() method when actually the click happens

Your fragment class where the view's click code is there.

myView.setOnClickListener(newView.OnClickListener(){
    @OverridepublicvoidonClick(View v) {
        mListener.onClickOfMyview();
    }
});

Solution 4:

You should create some method in your activity. Supposedly it will look like this :

public void startFragment(Fragment fragment) {
    fm.beginTransaction().replace(R.id.content_frame,new fragment).commit();
}

As you already done in youronNavigationItemSelected

Then, from your fragment, you can call

((MainActivity)getActivity()).startFragment(new SearchConsultanciesFragment()) 

for example

Solution 5:

Create a method switchFragment in your MainActivity

FragmentTransaction ft;

    publicvoidswitchFrag() {
            try {
                    ft = getActivity().getFragmentManager().beginTransaction();
                    ft.replace(R.id.frame_container, new Your_Fragment.class);
                    ft.commit();
                } else {
                    Log.e("test", "else part fragment ");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

Now from your current Fragment you can call this MainActivity's funciton throught the below code:

((MainActivity)getActivity()).switchFrag();

Let me know if this works for you! :)

Post a Comment for "Calling A Fragment From Fragment"