Proper top level navigations in android fragments like in Google Play - android

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();

Related

Android: how to create a custom back button for fragments in a KIOSK app

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!

Home screen launcher Android

I have 5 Fragments and of course my .MainActivity.
MainActivity
FragOne
FragTwo
---
FragFive
and then:
activity_main.xml
app_bar_main.xml
content_main.xml
nav_header_main.xml
lay1.xml
lay2.xml
---
lay5.xml
When the application is loaded it's a blank screen but then obviously when I click the navigation bar the pages will load.
My question is, is there anyway I can use a fragment as my launcher page, because if I tried making a home page on any other activity like content_main or acivity_main, obviously then it would show on every page which I don't want.
But then if I made a new activity and set that as the launcher,obviously that would work but then my navigation bar would be missing I presume? What's the best way to go about this?
What you want is not that big a problem. Just load a fragment using SupportFragmentManager in your MainActivity. If this Fragment is a one time view only (before you change to your other fragments using Navigation Drawer), then a simple loading of your launcher Fragment in the MainActivity is enough.
Example for loading it once in MainActivity:
In your onCreate method in MainActivity :
///////////Setting up Fragment as Starting Page////////////
LauncherFragment fragment = new LauncherFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,"launcher");
fragmentTransaction.commit();
This Launcher Fragment won't be seen after you change to other Fragments using NavigationDrawer (unless you call it somewhere else, which according to your question you don't want)
If instead of using separate LauncherFragment you decide you want to load FragOne itself, you will need to manually highlight your Drawer's First item (FragOne) after setting up FragOne
navigationView.getMenu().getItem(0).setChecked(true); ////Set Navigation drawer manually

Android fragment transition on back navigation to previous fragment

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().

Resume Fragments in Android

I am currently working on sample android navigation drawer application. It contains 1 activity and four fragments (import android.app.Fragment). I do not know how to resume the previous fragments, for example now I am in Fragment A here I did some activities like selected checkbox some thing like this. then I have moved to Fragment B, now if I press back button I want to resume the Fragment A with the previous state (with selected check box). please provide some sample code.
thanks in advance
As described in the documentation (http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments) you can add the fragment to the back stack:
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();

Viewpager inside fragment and back button

I asked this question before getChildFragmentManager() and viewpager without any responses, probably because the explanation was too long.
Simply put I have a viewpager inside a fragment. The pages inside that viewpager need to be replaced through a series of screens. Pressing back on those screens will not exit the application. How do I do this?
challenges:
viewpager xml does not use framelayout. when I use the replace method, what container
id do I use?
when replacing the fragments why would I use getChildFragmentManager each time if that is private to the current fragment?
#goonedroid: that's not the case I'm looking for here. The only similarity is that the fragment has a viewpager. My viewpager's pages need to be replaced when clicked:
I have a navdrawer. Clicking item 2 shows FragmentA. Clicking FragmentA replaces it with FragmentB. Clicking FragmentB replaces it with FragmentC. Clicking FragmentC takes you back to FragmentB. Fragments are added to the backstack for proper back navigation.
Clicking item 1 in the navdrawer shows FragmentZ with a viewpager. The viewpagers pages are just FragmentA and all the aforementioned behavior. But going from A to B here, then pressing back shows no pages in the viewpager.
I finally figured out a solution, not sure why this works because I thought an Activity's getSupportFragmentManager() returns the same fragment manager as its fragment's getFragmentManager().
Anyway, instead of getFragmentManager() in the fragments, use getActivity().getFragmentManager()

Categories

Resources