I'm using a ViewPager which has 6 fragments(i.e) 6 pages, i want to replace a fragment with a new fragment dynamically.. i'm using FragmentStatePagerAdapter and overriding getItemPosition method
the following is the code inside the getItemPosition method.
Fragment myFragment = (Fragment)object;
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
Fragment iviteFragment = new InviteFragment();
trans.detach(myFragment);
trans.add(mPager.getId(), iviteFragment);
trans.commit();
return POSITION_NONE;
this doesn't work.
Related
I have an activity with bottom tabs, which I use to switch fragments. One of the tabs have fragment with ViewPager setup (with 3 tabs). ViewPager has a RecyclerView and when I click on any item, the new fragment should replace fragment in which ViewPager exists.
But I receive an error No view found for id R.id.frame_layout_content for fragment IndexFragment when trying to replace fragment. How to properly replace fragment in this case?
Code flow:
Activity -> replace fragment in R.id.frame_layout_content with ViewPagerFragment -> Setup the ViewPager with fragment adapter (3 tabs) -> Click on RecyclerView item in IndexFragment to replace fragment in R.id.frame_layout_content with IndexDetailsFragment.
ViewPagerFragment:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
mViewPager = (ViewPager) view.findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new IndexFragment(), view.getResources().getString(R.string.title_index));
adapter.addFragment(new FaqFragment(), view.getResources().getString(R.string.title_faq));
adapter.addFragment(new QuotesFragment(), view.getResources().getString(R.string.title_cquotes));
mViewPager.setAdapter(adapter);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
super.onViewCreated(view, savedInstanceState);
}
Code to change fragment from IndexFragment:
IndexDetailsFragment newFragment = new IndexDetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.frame_layout_content, newFragment);
transaction.addToBackStack(null);
transaction.commit();
Try to change
IndexFragment newFragment = new IndexFragment();
to
Fragment newFragment = new IndexFragment();
Hope it helps.
R.id.frame_layout_content is inside MainActivity, so IndexFragment won't be able to access it.
You should make an interface, so IndexFragment can notify MainActivity and then MainActivity should change R.id.frame_layout_content content.
When I call FragmentTransaction transaction = getFragmentManager().beginTransaction() in IndexFragment (which replaced with FragmentManager attached to ViewPager) I receive an instance attached to ViewPager not to activity, which has R.id.frame_layout_content
So the solution is to get FragmentManager instance from parent Activity:
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
In my android activity I am using multiple fragments, I am successfully switching these fragments according to my requirement but problem is that when I am switching Fragment 1 to Fragment 2 and back from Fragment 2 to Fragment 1, Fragment 1 not showing previous data Fragment 1 start from stretch, but I want to show previous data same as I was selected.
Here is my code for go Fragment 1 (fragment_search_customerProfile) to Fragment 2 (fragment_CustomerInfo):
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, new fragment_Search());
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();
transaction.remove(new fragment_search_customerProfile()).commit();
Here is my code for back Fragment 1 (fragment_search_customerProfile) fromFragment 2 (fragment_CustomerInfo):
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Log.d("fragment_Search", fromSearch + "");
transaction.replace(R.id.custIndoFram, new fragment_search_customerProfile());
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.custIndoFram);
layout.removeAllViewsInLayout();
transaction.remove(new fragment_CustomerInfo()).commit();
Can anyone explain me how can I stay save my fragment data?
Instead of transaction.replace(R.id.filterFram, new fragment_Search());
you can use transaction.add to add fragment while having the data of first one
transaction.add(R.id.filterFram, new fragment_Search());
transaction.addToBackStack(null);
From fragment two you can use .show instead of replace to show the first fragment
and hide fragment two from the first one. transaction.show(getSupportFragmentManager().findFragmentByTag("firstFragmentTag")).commit();
You can simple keep the instances of the fragments, so in your Activity you would have fields like that.
private Fragment fragment1;
private Fragment fragment2;
And now you can use the sexy replace() option,
To add Fragment1
if (fragment1 == null) {
fragment1 = new fragment_Search();
}
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1);
and same for Fragment2
And you can hold the state of the fragment (data it retrieves) in the Fragment itself, and in your onViewCreated() you check, if the state is not null, you update the view immediately.
class Fragment1 extends Fragment {
private State state;
public void onViewCreated(View v) {
if (state != null) {
// Update UI here
}
}
}
UPDATE
Using your own code
private Fragment fragment1; // This is a field outside of below method
if (fragment1 == null) {
fragment1 = new fragment_Search();
}
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.filterFram, fragment1).commit();
FrameLayout layout = (FrameLayout) rootView.findViewById(R.id.cpFrame);
layout.removeAllViewsInLayout();
Going to fragment2 should be the same, and this code is used to go to Fragment1 and Fragment2, doesn't matter if you are going back or first time.
You have to add fragment instead of remove or replace
Short version:
I have activity with fragment, which include ViewPager (with FragmentStatePagerAdapter) and on resume to this fragment it recreates. How can I by back press return to Fragment and restore is state and it's Viewpager?
Details:
I have an activity with FrameLayout (R.id.themesFragmentsLayout), in activity's method onCreate(..) I do:
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ThemesFragment fragment = new ThemesFragment();
fragmentTransaction.replace(R.id.themesFragmentsLayout, fragment, ThemesFragment.TAG);
fragmentTransaction.commit();
}
ThemesFragment include only ViewPager with FragmentStatePagerAdapter, which holds 3 fragments:
public Fragment getItem(int position) {
switch (position) {
case ID_THEME_PAST:
return new ThemePastFragment();
case ID_THEME_OF_DAY:
return new ThemeOfDayFragment();
case ID_THEME_UPCOMING:
return new ThemeUpcomingFragment();
default:
return null;
}
}
In ThemeOfDayFragment for example, user can click some button and I change ThemesFragment (which holds ViewPager) with some another (for example ThemeDetails):
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.addToBackStack(newFragment.getClass().getSimpleName());
fragmentTransaction.replace(((AboutMyStyleApplication) getActivity().getApplication()).getCurrentFragmentsLayoutId(), newFragment, newFragment.getClass().getSimpleName());
fragmentTransaction.commitAllowingStateLoss(); // or fragmentTransaction.commit(); - same result
But when he came back (by back key press for example) ThemesFragment recreates, and all fragments in ViewPager recreates to.
In ThemesFragment I set setRetainInstance(true)
Can anyone help?
I'm trying to change a fragment inside a ViewPager:
public void change() {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, mapa);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mSectionsPagerAdapter.notifyDataSetChanged();
}
R.id.pager is my ViewPager id.
mapa is a inflated fragment.
If I call change(), the fragment I want to replace will disappear but the new fragment will not be load.
The view pager uses an Adapter to change the fragments it displays. Just like a ListView uses its adapter to get views to populate the list.
You have to extend FragmentPagerAdapter, create a new instance of it and set it to the pager with this line:
mViewPager.setAdapter(fragPagerAdapter);
I want to replace a Fragment from a defined class FluxListeFragment with another class FluxDetailFragment (and use addToBackStack fonctionnality of the FragmentTransaction). Thus, I add my fragments dynamically on the onCreate of my activity :
FluxListeFragment FragmentListe = new FluxListeFragment();
fragmentTransaction.add(R.id.FL_Portrait, FragmentListe, "fragment_portrait");
fragmentTransaction.commit();
Then, when I want to replace FragmentListe with FragmentDetail, I code this :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FluxDetailFragment FragmentDetail = new FluxDetailFragment();
fragmentTransaction.replace(R.id.FL_Portrait, FragmentDetail, "fragment_portrait");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
But I get an InvalidClassCastException, since I want to replace a Fragment from a class with a fragment from another class.
How to make this work ?