I have 2 fragments the first contains a button which on clicked opens a fragment with a ListView in it. I have a shared element transition for the button to transition into the new fragment (root layout) but I would also like to have this transition in reverse (the list fragment contracts into the button again).
However currently I detect the list item click and send an event to the Activity which pops the listview fragment off the backstack (popBackStackImmediate()) hence does not show the transition.
Is there a good way to allow back navigation while preserving reverse transition to work as well?
did you try this:
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack();
fm.executePendingTransactions();
PS: use getSupportFragmentManager() if you're using the support library else use getFragmentManager().
Related
I CANT use ActionToolbar because this is a KIOSK app with a custom header fragment that needs to be displayed at all time. Hence my situation, I have a basic ImageButton on the said header fragment, and I want to implement the functionality of a basic ActionToolbar.
I add my fragment to the main container like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, UserListFragment.newInstance())
.addToBackStack("foo")
.commit();
The question is, how do I restore the state of my main_container before the current fragment was replaced with UserListFragment.
Like : fragment A is showing, replacing fragment A with B, pressing some button in my header fragment to pop the fragment B so again fragment A is showing.
You should consider switching to Android Jetpack Navigation Component it was created for your type of situation.
Check: https://stackoverflow.com/a/62252603/8714139
Cheers!
I have an activity with a fragment container layout and in that layout i load a fragment A which has a listview (listview loads data from web). Now on clicking listview item i want to show detail view. For that i am loading another fragment B which is detail fragment. Now to do that first i used
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
But using replace remove the previous fragment A which contain listview and on pressing back it again reloads the onCreateView method of fragment A and its listview data. I want to prevent it from loading fragment A again (or sending call to webserver) on backpress. I want to maintain fragment A state and its listviews scroll position. To do that i used second option which is
FragmentTransaction ft = ((FragmentActivity) context).getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragmentsContainerLayout, new ScheduleDetailsFragment());
ft.addToBackStack(null);
ft.commit();
If i use add() then Fragment B loads on top of Fragment A and Fragment A still visible. It shows both fragments overlapping each other and transparent. I also came across anothe solution which is to use show() and hide() methods. But i dont think that is an efficient way to achieve that. Can anyone suggest an effective solution?
I have an Activity that contains two fragments beside each other. One of the fragment is a List Fragment and the other one contains surface view for a video.
I've implemented a custom controller to make the video in full screen but when the full screen button is pressed the list fragment still exist. How can i show/hide the list fragment when the toggle full screen button on the video fragment is pressed?
If the event of video's full screen occurs inside fragment then you can write an interface which will be implemented by parent activity hosting the two fragments. The interface will contain a function that will be called after you make the video full screen.
On the function's implementation in Activity remove the list fragment using FragmentManager and FragmentTransaction class.
You can remove listFragment fragment like this;
FragmentManager fragmentManager = getFragmentManager()
Or:
FragmentManager fragmentManager = getSupportFragmentManager();
Then;
fragmentManager.beginTransaction().remove(listFragment).commit();
Similarly, when full screen mode is off then you can add this fragment back the same way using add() function instead of remove().
I have an Activity with navigation drawer and a default fragment set in to Activity when application starts.
I have 4 top level navigation in my navigation drawer
Fragment 1
Fragment 2
Fragment 3
Fragment 4
and switching the fragments inside the activity on click on each each navigation. I want implement the fragment navigation in such manner that from each top level navigation fragment, if user clicks back button it should first come to Main or default fragment and from there app should exit same like in Google Play. I call it master fragment.
eg:
Default(master) Fragment > Fragment 1
Fragment > Fragment 2
Fragment 2 -- Back pressed > Deafult fragment and like so.
What I have tried so far :
I have tried adding fragment in backstack but it doesn't help it takes me all fragment in stack.
getSupportFragmentManager().beginTransaction()
.add(R.id.container, selectedFragment)
.addToBackStack("naviagtion_stack")
.commit();
My each top fragments also have child fragments in stack so stack count also did not help me.
I don't want to remove and add my default fragment because as it fetches some data from network so recreation will make the network call again which i don't want .
I want exactly what Google Play does. I just want to know the logic.
Add your master fragment to backstack and remember the tag: fragmentManager.beginTransaction()
.add(R.id.main_layout, masterFragment)
.addToBackStack(INITIAL_STATE)
.commit();
Click on the navigation elements should do following before adding a corresponding fragment: fragmentManager.popBackStack(INITIAL_STATE, 0);
This call removes from backstack everything but your master fragment.
All fragment transactions (including navigation fragments) should generally do the same thing, for example:
fragmentManager.beginTransaction()
.add(R.id.main_layout, fragment)
.addToBackStack(null)
.commit();
I am working on an application that has three fragments which are defined in an XML file:
[HeaderFragment]
[MainFragment]
[FooterFragment]
The first screen initiates the three fragments, the Header- and FooterFragment are static so will not change content.
The MainFragment is initial a menu with buttons and a transparant background (MenuFragment). When I click an item in the menu I replace the MenuFragment with a new fragment (DetailsFragment) like this:
FragmentTransaction transaction = mFragmentManager.beginTransaction();
Fragment newFragment = new DetailFragment();
transaction.replace(R.id.content_container, newFragment);
transaction.addToBackStack(newFragment.getTag());
transaction.commit();
The DetailFragment shows up and when I press back, the MenuFragment appears and everything works how it should.
Here is my problem:
Inside my DetailFragment, I have a toggle option to filter the content, this is a button. When this is clicked, the DetailFragmentFiltered replaces the DetailFragment on the same way as the code above states. The only difference is that I don't add it to the BackStack because after filtering and pressing Back.. I still want to return to the MenuFragment.
When I clicked the filter button and press Back, the DetailFragment (or DetailFragmentFiltered) is shown behind my MenuFragment. Afcourse, I don't want this.
Make sure you don't use a static fragment relation to an XML by setting the first fragment as "android:name" in the layout.
Make with framelayouts the layout of the XML and add the fragments flexibly as shown in this tutorial:
http://developer.android.com/training/basics/fragments/fragment-ui.html