How do I open a new fragment from another fragment?

android, android-button, android-fragments, onclicklistener

Solution

Add following code in your click listener function,

NextFragment nextFrag= new NextFragment();
getActivity().getSupportFragmentManager().beginTransaction()
             .replace(R.id.Layout_container, nextFrag, "findThisFragment")
             .addToBackStack(null)
             .commit();

The string `"findThisFragment"` can be used to find the fragment later, if you need.

Problem

I tried making a navigation between fragments. I've got the `NewFragment.java` with the new fragment working. My problem is: How do I make this `onClickListener` run `NewFragment.java` correctly? ``` button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(getActivity(), NewFragment.class); startActivity(i); } }); ``` FYI: This is from inside a fragment (I don't know if that matters).

Original source