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).
Use this,
Add following code in your click listener function,
The string
"findThisFragment"
can be used to find the fragment later, if you need.Just to be precise,
R.id.viewpagerId
is cretaed in your current class layout, upon calling, the new fragment automatically gets infiltrated.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.
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,
Hope this will work. Sometimes you may get getSupportFragmentManager() instead of getFragmentManager().