I'm using a ViewPager together with a FragmentPagerAdapter to host four different fragments.
What I'm trying to achieve is to successfully replace Fragment1 with a whole new fragment, newFragment,on click of a button.
When I use..
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment1_layout_id, newfragment);
transaction.remove(fragment1);
transaction.commit();
The fragment is replaced and Fragment is shown instead of Fragment1. Though as soon as I swipe all the way to Fragment4 and then back to newFragment, Fragment1 has made a comeback.
Your fragments are provided by FragmentPagerAdapter.
You would need to adapt what fragment your FragmentPagerAdapter creates for your Fragment1 and then call notifyDataSetChanged() on the adapter.
With your approach, the time Fragment1 should be shown again, the ViewPager will simply ask your FragmentPagerAdapter to re-create the specific instance - which will give you back the instance you were trying to replace.
Not that ViewPager can cache fragments internally, as long as notifyDataSetChanged() was not called on the adapter. Hence, it may not pick up chances you have made without that call.
Related
When I tried to use transitions with fragments using similar code from this article on Medium GitHub. It wouldn't work when I am using the add method() on fragment manager but it would work with replace().
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.addSharedElement(mSharedElement, "transName")
.add(D.id.content_frame, fragment, FRAG_NAME)
.addToBackStack(null)
.commit();
I need to use add() method to keep the fragment below as the user will be navigating back and forth between the first fragment which contains a list of items and the second fragment show detailed info about the item. I don't want to keep loading the fragment with the list again.
Is there a way to get shared element transitions to work when using add() on the fragment manager, than replace().
Working with fragments I've always used replace() for my transactions, but I wish I didn't have to save instance states anymore to restore a fragment's view and prevent reloading when coming back to that fragment. So, I've decided to work with add(). The thing is when I add another fragment, the previous fragment view remains in the background and that's fine (that's the behavior I expected), but the problem is I can actually interact with the views in the background. Example:
Fragment A has a Button
Fragment B has a TextView
When I add Fragment A and later add Fragment B, I'm able to click on Fragment A's Button, even staying on Fragment B's view.
I'm using:
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction().
add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());
if (shouldGoBack)
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
where getRootViewContainer() returns the id of the FrameLayout I'm using as my activity main container.
Now, is it really the default behavior of add()?
If so, is there a proper way to avoid this or one just has to use replace()?
What you can do here is just hide previous fragment at the time of transaction of current fragment.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment newFragment= new MyFragment ();
ft.hide(CurrentFragment.this);
ft.show(newFragment);
ft.commit();
It worked for me just try it.
FragmentTransaction.hide(fragmentBehind); //works for me!
example :
//I have it globally available
FragmentTransaction trans = MainActivity.getManager().beginTransaction();
//not globally
FragmentTransaction trans = getFragmentManager().beginTransaction();
MapFragment newFragment = new newFragment();
trans.add(R.id.fragmentContainer, newFragment, tag);
trans.hide(this);
trans.addToBackStack(tag);
trans.commit();
Yes, this is a default behaviour of add().
If you really don't want to user replace(), you can try to disable views which are inside "old" fragment.
I have used View pager in my app for my list fragments (A,B,C,D). On clicking on an item in listview (of say Fragment A) , a detailed fragment (E) is shown. Fragment E is not part of view pager for obvious reasons.
At this point, if i use the getFragments() method of fragmentmanager, i only get the view pager fragments (A,B,C,D) and not E. On iterating through view pager fragments,one of the fragment of view pager comes out to be visible,although the fragment E is on top and visible.
What is the correct way of replacing a view pager fragment with a non-view pager fragment? Why is Fragment E, not added to fragment manager stack?
Here is the code segment that i use while replacing Fragment A with Fragment E.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.detach(fragA);
fragmentTransaction.add(viewId, fragE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Why i am doing this is, because on some async calls completion, i would like to refresh the fragment which is currently visible to user.
Thanks,
Rony
have you tried replace?
FragmentTransaction fragmentTransaction =getActivity().getSupportFragmentManager().beginTransaction();
// Replace the fragment in the container view with the E fragment,
// and add the transaction to the back stack
fragmentTransaction.replace(R.id.container,FragmentE.newInstance(),MainActivity.FRAGMENT_TAG_E);
fragmentTransaction.addToBackStack(null); // Commit the transaction
fragmentTransaction.commit();
Replacing the fragments looks to be an inefficient solution as it recreates the fragment unnecessarily. For now I have solved it by maintaining a list of my own for all the fragments.
I am develop the app using the fragments and i am facing one fragment1 adding to the another fragment2
fragment1 onclicks performing fragment2 .I didn't findout the solution for that. please guide anyone know
I am using the following code to add the fragment
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager = activity.getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.container,fragment2);
ft.hide(new Fragment1());
ft.addToBackStack(Fragment1.class.getName());
ft.commit();
The above to use adding onefragment1 adding to fragment2.Fragment1 onclicks perform to fragment2 . i didn't findout mistake.
please guide me anyone know .Advance thanks to all
nested Fragments are not recommended and for adding a fragment to a container from another Fragment use the parent Activity to do so , you can define a function inside Parent Activity which replace current Fragment with your second Fragment and call it from your first.
Adding a fragment to another fragment is the concept of nested fragments and not recommended. You should replace the fragment instead of adding. Use the following:
Fragment fragment = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().addToBackStack("fragment").replace(R.id.frame_container, fragment).commit();
I use a holder activity with FrameLayout.
There I put a fragment with a listview. It works fine.
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, feedFragment);
ft.commit();
Then I add another fragment.
android.support.v4.app.Fragment targetFragment = new MainPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use add() instead of replace() to return to previous position of the listview when hitting back key. It works fine.
But it is possible to navigate to the third fragment from the second fragment.
android.support.v4.app.Fragment targetFragment = new FullPhotoFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragments, targetFragment);
ft.addToBackStack(null);
ft.commit();
Here I use replace to force the 2nd fragment to reload when hitting back key.
Sometimes back key from the third fragment works fine, it displays the second fragment that is reloading on appearing.
But sometimes (as I can see it happens first time when I try this steps) hitting back key from the third fragment leads me to the first fragment, closing the second fragment against my expectations. And the first fragment is reloading.
How to prevent this strange behavious?
add() method will add Fragments to Container and any other fragments added to the Container will be queued back of the first fragment. They will be not visible until and unless first fragment made Invisible. I hope this is the problem you are facing. It would be good if you use replace() for the first-->second fragment navigation also.