I want to create fragment in activity. I have 4 fragment in 1 activity I have a code like this
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new fragment_invoice(), "Invoice");
adapter.addFragment(new fragment_sales_order(), "Sales Order");
adapter.addFragment(new fragment_achievement(), "Achievement");
adapter.addFragment(new fragment_approval(), "Approval");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
I am has been success to show fragment but when I open other fragment for example 1. open fragment_invoice 2. move to fragment_achievement 3. I go back to fragment_invoice and then all data will be lost but in fragment_sales_order not. 4. It was happen to if I open fragment_sales_order 5. Move to fragment_approval and then all data in fragment_sales_order will be lost.
Why data in fragment lost with odd even fragment ?
I was research about this and maybe give me some idea for use getchildfragmentmanager. getChildFragmentManager () cannot be resolved or cannot be referenced
for method getchildfragmentmanager must be extends to FragmentManager but my java class must extends AppCompatActivity. Cannot extends to FragmentManager because some data I get from http on extend to activity intent
can someone help me to fix my fragment for show data always when I move to other fragment ? sorry if my question and my grammar weird 감사합니다 여러번
The definition of getChildFragmentManager() is:
Return a private FragmentManager for placing and managing Fragments inside of a Fragment.
Meanwhile the definition of getFragmentManager() is:
Return the FragmentManager for interacting with fragments associated with the fragment's activity.
Basically, the difference is that Fragment's now have their own internal FragmentManager that can handle Fragments. The child FragmentManager is the one that handles Fragments contained within only the Fragment that it was added to. The other FragmentManager is contained within the entire Activity.
To display Fragment1 on MainActivity we must use getSupportFragmentManager()
getSupportFragmentManager().beginTransaction().replace(R.id.container_view_on_main, Fragment1.newInstance());
To display Fragment2 from Fragment1 we have 2 way
Use getFragmentManager()
getFragmentManager().beginTransaction().replace(R.id.container_view_on_main, Fragment1.newInstance());
Use getChildFragmentManager()
First, we have to create a layout with id container_view_on_fragment1 inside fragment1.xml, then
getChildFragmentManager().beginTransaction().replace(R.id.container_view_on_fragment1, Fragment2.newInstance()).commit();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With