Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing Instagram like in-app navigation system on Android

Tags:

java

android

I have to implement a navigation system similar to the one used in the Instagram Android client.

  1. There should be a permanent tabbar on the bottom of the screen all the time.
  2. When the user navigates deeper within one of these tabs, lets say to a detail view, then switches to another tab, and then switches back to the previous tab, the last shown (deeper) detail view should be shown, and on back presses, it should be iterating back till the main view of the said tab.

What I have came up with so far is the following:

  • I have a MainAcitvity showing the menu on the bottom.
  • On selecting a menu point, the appropriet Fragment is shown.
  • When the user navigates further within a Fragment, it then asks the MainActivity to change its content by the given criterias, resulting in changing the Fragment shown.
  • I add all the Fragment changes to the backStack, by calling the FragmentTransaction's addToBackStack() method.

I am stuck at this point, and cannot figure out how to switch fragments on back presses, and how to handle tab navigations when deeper views are shown instead the main views of the tabs.

I am thinking of using my own separate "backstack implementations" for every tab. When the user navigates deeper within a tab, i generate a unique "tag" and use that tag when calling addToBackStack() and also putting the tag in the "backStack" implemented by me. In case the user navigates again to this tab, i can check if i have any tags in the "backStack" for that tab, and if so, then look up that entry in the real backStack in the fragmentManager of the MainActivity, and switch to it.

I could not come up with anything better yet. Is there any better/simpler way to attchieve the said behaviour? Am i missing something? (I know this is really bad application design in the Android world, but it is another question)

like image 758
Csaba Szugyiczki Avatar asked Apr 12 '14 22:04

Csaba Szugyiczki


1 Answers

I am posting an answer since the question is pretty dead, yet the conclusion might be helpful for others.

We ended up sticking with the old fashioned NavgationDrawer pattern, which worked well. But in the meantime I had to implement a library project which provided a Fragment to the hosting Application which had its own custom logic. This Fragment then used its ChildFragmentManager, to add another Fragments inside itself. ChildFragmentManager is ported back in the Android Support v4 lib, so you can use it basicly everywhere.

So lets say you want x Menu points in which you can navigate deeper. Those will be the Fragments using their own ChildFragmentManagers to add other Fragments to navigate deeper within that Menu. ChildFragmentManagers have their own back stack, so you dont have to worry that much about handling states. If another Menu is selected, you can look up the corresponting Fragment in the MainActivitys FragmentManager, and change back to it, or add it if it has not yet been added.

Be careful, you have to implement back functionality yourself, since ChildFragmentManagers will not get the backPressed event automatically. You can do this by handling the onBackPressed event in your MainActivity.

@Override
public void onBackPressed() {
  boolean handled = false;
  if(getFragmentManager().getBackStackEntryCount() == 0){
    // No menu added
    return super.onBackPressed();
  }

    Fragment frag =    getFragmentManager().getBackStackEntryAt(getFragmentManager().getBackStackEntryCount() - 1);

  if (frag instanceof XMenuFragment && frag.isVisible()) {
    FragmentManager childFm = frag.getChildFragmentManager();
    if (childFm.getBackStackEntryCount() > 0) {
      // pop the last menu sub-Fragment
      childFm.popBackStack();
      handled = true
    }
  }

  if(!handled){
    super.onBackPressed();
  }
}

I used a different code, so it might contain errors, but i hope that the point of the concept is clear.

like image 182
Csaba Szugyiczki Avatar answered Oct 29 '22 13:10

Csaba Szugyiczki