calling previous fragment from another fragment inside navigation drawer - android

Hi I have a navigation drawer and I have 4 fragments. I want to go from fragment 3 to fragment 2 on back press and on back press from fragment 4 it should go to fragment 1.How can I do this ??
I have tried adding fragments to back stack. If I add fragment to back stack when I click from home fragment the app should actually close.Instead of closing the fragment which I added to backstack is showing and when back pressing from that it showing the home fragment again and then the app is closing.

//try this code add fragment number while opening new fragment and remove last positions from the list on backpress
public ArrayList<Integer> list = new ArrayList<>();
public void addToStack(int pos)
{
if (!list.contains(pos))
{
list.add(pos);
}
}

Related

Returning to previous tab from another activity in android app

I have this android app in which my main activity HomeActivity holds two fragments using a ViewPager and a tab layout. Each fragment holds a list of items and when clicked takes the user to another activity. Now I would like to return to the previous tab layout when the back button is pressed in the current activity. Simply put how do I get back to the tab layout from which the user had come from. Normally a back press would take the user to a default tab, in this case the BtcFragment tab but I would like to be able to return the user to the originally clicked tab in the HomeActivity.
in your Main Activity or where you define your view pager create a static method as below :
public static void ViewPagerTabSelector(int tabNumber) {
mViewPager.setCurrentItem(tabNumber);
}
then from activity you want to back in your `onbackpress call:
#Override
public void onBackPressed() {
MainActivity.ViewPagerTabSelector(0);
}

Open inner fragment on clicking a button

In My application I have 5 tabs.
In 1st tab when user clicks a button I need to go to 2nd tab which contain 3 fragments.
1) List Fragment
2) Details Fragment and
3) Result Fragment
When user click on a button I want to go directly to the Details Fragment, and when clicks back it should go to the list.
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();
In your code instead of calling getActivity().getSupportFragmentManager() call getActivity().getchildfragmentmanager()
Fragment fragment = ListDetailsFragment.newInstance(((MyApp) MyApp.getContext()).getCurrentItemsList().get(0));
getActivity().getChildFragmentManager().beginTransaction().add(R.id.activity_fragment_container, fragment,
fragment.getClass().getSimpleName()).commit();

Fragments navigation

I need to create navigation of fragments like in Gmail app. It's like: we have one main fragment A, we can open another fragment (B,C,D...) from navigation drawer, and when we open new fragment, it`s open on top of main fragment, and when press back button, in all cases we come back to main fragment A, don't depend of count new opened fragments. It's seems, first main fragment A we use add method(int FragmentTransaction) without adding to fragment backStack. Then, next fragment B we use method add too, with adding to back stack. And when I need to open another one (Fragment C), I need to replace second fragment B. But, when I use method replace(), replaced all container, and main fragment A not showing when back button pressed from fragment C or B and app close. So, the question is: how to replace only fragment B or C, without losing fragment A?
A valid solution would be to have two container framelayouts in your activity. The first one (which will below the other one) contains your fragment A. Everything you open will be added/replaced in the second container.
Another solution is to include the fragment A statically in your layout and have your container framelayout on top of it where you add your fragments B, C, D etc.
open fragment Like this
HighlightFragment highlightFragment=new HighlightFragment(FirstReaderScreen.this); //Your fragment
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, highlightFragment) // LL_fragment is container
.addToBackStack(null)
.commit();
and in Activity OnBackPress
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Using Fragments recover Home/Drawer Button

I have an app with a main activity which loads a navigation drawer, and a pair of fragments that load in that activity ...
In the navigation drawer I have 4 options A, B, C and D ... the first one loads FragmentA on my activity and the last 3 load FragmentB ..
FragmentA displays a list of elements and, upon selecting one of these elements FragmentB is used to load its content... I want to change the home (hamburger/drawer) icon on FragmentB for the up icon when initiating from FragmentA (and change the corresponding behavior to make a popstack on select).. I have no problem with this using setDisplayHomeAsUpEnabled(true), but since all this is occurring inside one activity if I then select one other option (say B) from the navigation drawer the up icon will still be showing (it its also showing on the popped fragment)...
if I use setDisplayHomeAsUpEnabled(false) all this do is hide the home/up button from the toolbar, I need to recover the home button and make sure this will be shown when FragmentB is initiated from the drawer menu ...
Does this problem ring a bell to anyone? or am I just using fragments the wrong way? .. any advice will be appreciated
EDIT
this is more or less what I have in code
In Main Activity .. as the onNavigationItemSelected(MenuItem item) for the drawer I have a something like this ...
switch(optionNumber) {
case 0:
fragment = FragmentA.newInstance(optionNumber);
break;
default:
fragment = FragmentB.newInstance(optionNumber);
break;
}
Fragment frag = fragmentManager.findFragmentByTag("current_fragment");
if (frag != null && frag.getClass() == FolderFragment.class){
((FolderFragment)frag).resetScroll();
}
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.content, fragment, "current_fragment").commit();
which selects the fragment to load according to the option selected..
In FragmentA I'm calling FragmentB with this ..
FragmentB fFragment = FragmentB.newInstance(position);
Bundle args = new Bundle();
args.putString("filter", "something"); fFragment.setArguments(args);
mActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.flContent, fFragment, "current_fragment")
.addToBackStack(null)
.commit();
Preserving the fragment in the stack
And in fragmentB inside onResume() function I got something like...
String filter = getArguments().getString("filter", null);
if (type != null) {
mActivity.setTitle(title);
mActivity.getSupportActionBar().setDisplayShowHomeEnabled(true);
}else {
/*mActivity.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
mActivity.getSupportActionBar().setDisplayShowHomeEnabled(true);
mActivity.getSupportActionBar().setHomeButtonEnabled(true);
mActivity.getSupportActionBar().setIcon(R.mipmap.ic_menu);*/
}
So When I'm creating fragmentB I check for arguments and see if it comes from fragmentA or not ( I could also check the fragmentmanager backstack and see if there's something)... there I just change the drawer icon with setDisplayShowHomeEnabled(true) ... leaving the back arrow, if I return to FragmentA (via onBackPressed()) FragmentA shows the arrow and I need it to show the original drawer icon ... the same happens if I select an option from the drawer menu ...
Does this gives more clarity to my issue ?... I have some commented code there because it doesn't work .. if I activate the line with setDisplayHomeAsUpEnabled(false).. the icon just disappears from the activity (which is the intended result of the function as far as I know)...
After a while I finally found this post
Switching between Android Navigation Drawer image and Up caret when using fragments
I guess that when involving a Drawer in the interface you might need to handle this issue with that component... this post gave me the answer.
Particular notice to the last comment by Wolfram Rittmeyer

Save state navigation drawer page (fragment)

I've had a search on SO and can't find anything that works.
Here's what I want to do, when I navigate between navigation drawer pages, I want the view to be the same. So if I go from page A to B, and then from B to A. I expect A to have the same state.
In my app, I add some text to fragment A via some button. When I navigate to fragment B, and then back to A, the text is gone. I want the text to still be there.
How do I do this?
I've already tried setRetainState(true). This doesn't work. I have also made sure that new Fragment() is only called once when going to the page. This also doesn't work.
The structure of my navigation drawer is identical to the one shown in the Android documentation.
I think, if you have a navigation drawer that you use fragments for each page.
On select item in navigation drawer don't recreate the fragment, just pop it from back stack if it exists.
HomeFragment homeFragment = (HomeFragment) fm.findFragmentByTag(fragmentName);
if (homeFragment == null) {
homeFragment = new HomeFragment();
fm.beginTransaction().addToBackStack(fragmentName).replace(R.id.content_frame, homeFragment, fragmentName).commit();
} else {
fm.popBackStackImmediate(fragmentName, 0);
}

Categories

Resources