How to reload a Viewpager's Fragment from an activity - android

I have an activity in which layout has Tabs using TabLayout and viewpager from fragments. There are two tabs and having 2 fragments 1 for each tab.
Now I am on second Tab's Fragment, when I perform any action in this fragment, I have to reload that fragment, but when I am restarting that activity using the below code:
((Activity)mContext).finish();
mContext.startActivity(((Activity) mContext).getIntent());
It reloads the first fragment, I want to reload the second fragment, how can I achieve this.
Thanks you so much for your help.

Try to refresh your current Fragment:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();

Related

How to replace fragment that is loaded by navigationView

I have a navigation view that loads some fragments depending on which is pressed. In one of the fragments, I want to have a button, which when it is pressed, the fragment changes from the one loaded by the navigation view to a different fragment. I have tried using
ProfileEditFragment pef = new ProfileEditFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().remove(pef).replace(R.id.drawer_layout, pef, "Fragment")
.addToBackStack(null).commit();
When I do this, it loads the other fragment on top of the current fragment, rather than replace it, even though I used the replace function. How do I fix this?
Can you try this:
ProfileEditFragment pef = new ProfileEditFragment();
FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.drawer_layout, pef);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();

Remove single fragment from backstack Android

I am struggling to remove a single fragment that I add dynamically when I have multiple fragments.
For examples:
MainActivty
Inflate FragA
Inflate FragB
Inflate FragC
Now how do I just delete fragment A?
Using popBackStack kills all three and getSupportFragmentManager().beginTransaction().remove(TAG).commit(); also seems to do thae same thing
How are you meant to do this correctly? I am trying to keep multiple backstacks persistent over tabs
try this...
get current fragment title & check fragment A or not.
get current fragment title to use getTitle() Method and check
if(getTitle().toString.equals(fragment A){
// do
}else{
// do
}
Can you try this..
For eg. using fragment name as tag:
FragmentA fragment = new FragmentA();
String backStateName = fragment.getClass().getName();
Adding to backstack:
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(backStateName);
ft.commit();
Popping:
getSupportFragmentManager().popBackStackImmediate (backStateName, 0);
This should only pop the fragment with the specific tag.

Two android ViewPagers in the fragment backstack are not coming up properly after activity is recreated

My Android app has only one activity and all the fragments are added or replaced in one framelayout.
The issue is when I have two different viewpagers in the fragment backstack and the activity is recreated the second viewpager comes up as the first viewpager.
Below is how I add the fragments to the backstack in the order that they happen so 3 is the fragment that comes up after the activity is recreated.
setupPlacesEventMainView(), this is a viewpager which uses a FragmentStatePagerAdapter.(shows up properly after activity is recreated)
setupPlaceDetailViewPager, this is a viewpager which uses a FragmentStatePagerAdapter. (ISSUE: PlacesEventMainViewPager shows up here instead of PlaceDetailViewPager. The issue occurs even if I don't navigate into the EventSpecials)
setupEventSpecials(), this is a listfragment. (shows up properly after activity is recreated)
Add PlacesEventMainViewPager to backstack
public void setupPlacesEventMainView()
{
clearBackStack();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
PlacesEventMainViewPager placesEventMainViewPager = new PlacesEventMainViewPager();
transaction.replace(R.id.fragment_container, placesEventMainViewPager, PlacesEventMainViewPager.class.getSimpleName());
transaction.commit();
}
Add PlaceDetailViewPager to backstack
public void setupPlaceDetailViewPager(Event event) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
PlaceDetailViewPager placeDetailViewPager = new PlaceDetailViewPager();
placeDetailViewPager.setEvent(event);
transaction.add(R.id.fragment_container, placeDetailViewPager,PlaceDetailViewPager.class.getSimpleName());
transaction.addToBackStack(PlaceDetailViewPager.class.getSimpleName());
transaction.commit();
}
Add SpecialsList to backstack
public void setupEventSpecials(Agenda[] agendas) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
SpecialsList specialsList = new SpecialsList();
specialsList.setAgendas(agendas);
transaction.add(R.id.fragment_container, specialsList,SpecialsList.class.getSimpleName());
transaction.addToBackStack(SpecialsList.class.getSimpleName());
transaction.commit();
}
What the fragments contain.
PlacesEventMainViewPager contains 3 listfragments.
PlaceDetailViewPager contains 2 listfragments
SpecialsList is 1 listfragment.
I can reach the PlaceDetailViewPager fragment by clicking on one of the items in the PlacesEventMainViewPager fragment. I can reach the SpecialsList by clicking a button in the PlaceDetailViewPager fragment.
Please let me know if you need any more details. Thanks!
You never add PlacesEventMainViewPager to the backstack which makes PlaceDetailViewPager the first one in the backstack if you listed them in order.

Open a fragment, not in the current list of tabs, in a fragment

I want to open a fragment, which does not lie in the list of tabs, from a fragment in an onClick() function.
I have been struggling with this for a while now. Can someone give some pointers?
All the solutions that I have seen transition from one fragment to another(both being part of the tab list).
Thanks.
Create a instance of the fragment and use fragment Manager
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LinkListFragment llf = new LinkListFragment();
ft.replace(R.id.container, llf);
ft.commit();

Restart fragment inside activity

I have a litle doubt.
I have an activity that have 3 fragment inside. I need to restart the state of one of these fragments. Restart only one.
Old, but may be useful to someone else. To refresh the fragment, you need to detach the fragment and reattach it
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Your_Fragment_TAG is the name you gave your fragment when you created it

Categories

Resources