How do I open a new fragment from another fragment?
-
21-12-2019 - |
Question
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).
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.
OTHER TIPS
This is more described code of @Narendra's code,
First you need an instance of the 2nd fragment. Then you should have objects of FragmentManager and FragmentTransaction. The complete code is as below,
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager=getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_main,fragment2,"tag");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Hope this will work. Sometimes you may get getSupportFragmentManager() instead of getFragmentManager().
You should create a function inside activity to open new fragment and pass the activity reference to the fragment and on some event inside fragment call this function.
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
UserResult nextFrag= new UserResult();
this.getFragmentManager().beginTransaction()
.replace(R.id.content_frame, nextFrag, null)
.addToBackStack(null)
.commit();
}
Use this,
AppCompatActivity activity = (AppCompatActivity) view.getContext();
Fragment myFragment = new MyFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
Fragment fr = new Fragment_class();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.viewpagerId, fr);
fragmentTransaction.commit();
Just to be precise, R.id.viewpagerId
is cretaed in your current class layout, upon calling, the new fragment automatically gets infiltrated.
Adding to @Narendra solution...
IMPORTANT: When working with fragments, navigations is closely related to host acivity so, you can't justo jump from fragment to fragment without implement that fragment class in host Activity.
Sample:
public class MyHostActivity extends AppCompatActivity implements MyFragmentOne.OnFragmentInteractionListener {
Also, check your host activity has the next override function:
@Override
public void onFragmentInteraction(Uri uri) {}
Hope this helps...
Well my problem was that i used the code from the answer, which is checked as a solution here, but after the replacement was executed, the first layer was still visible and functionating under just opened fragment. My solution was simmple, i added
.remove(CourseListFragment.this)
the CourseListFragment is a class file for the fragment i tried to close. (MainActivity.java, but for specific section (navigation drawer fragment), if it makes more sense to you) so my code looks like this now :
LecturesFragment nextFrag= new LecturesFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.remove(CourseListFragment.this)
.replace(((ViewGroup)getView().getParent()).getId(), nextFrag, "findThisFragment")
.addToBackStack(null)
.commit();
And it works like a charm for me.