I am a beginner in android. I have created Tabs using ActionBar tabs. Now I want to implement, when the user in in first tab and clicks back Button activity should close or when the user is in second tab, he clicks the backbutton the app should show the first tab, which will make it as the home tab. I tried using popupbackstack() but it always closes the activity no matter what tab i am in.
please guide me. Thanks in advance.
The easiest way would be record the tab state (i.e., which tab is showing) in the host activity, and override the onBackPressed() method to do what you want.
Some tips:
remember to save the state(override the onSavedInstanceState() and save the tab state) to make you activity works right after restore from onPause().
read about android's recommended navigation style here. What you designed is a little bit different from what android suggests.
Related
My Action bar consists of two tabs (a list of businesses and a map of businesses). If someone chooses from the list I would like to hide the tabs and show that business' page. If the user hits back, the business fragment is popped and the tabs should be displayed again.
What is the best approach to get this working? So far with the following code I have an inexplicable recursive loop if I pop the business fragment :(
So picture this, I'm displaying the BusinessListFragment tab. I choose a business, swap fragments and in onPause() I set the navigation mode to standard. Hit the back key, and in the onResume() of the BusinessListFragment I have this:
ActionBar ab = mHostingActivity.getSupportActionBar();
if (ab.getNavigationMode() == ActionBar.NAVIGATION_MODE_STANDARD)
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Which is supposed to bring back the tabs, but the app freezes with a loop. The tabs are definitely still present. Maybe I'm doing something else strange in my code (I'm sure I'm not though), or maybe Android does something strange when the naviagtion mode is set?
Edit: I've learnt that simply setting the navigation mode to tabs seems to actually select the first tab - which I believe would explain the recursion. Interesting!
Okay well this took a while to figure out but hopefully this will help future googlers :D
It seems the best practice is to never try and hide / show tabs by setting the navigation mode from within a tab fragment's onResume / onPause, or anywhere within it for that matter.
I therefore set the mode to Standard in the onResume() of the detail (business) fragment. Normally if there are no more hierarchical layers of navigation to traverse from within the detail fragment, it is fine to simply set the mode back to Tabs in the detail fragment's onPause() - accounting for back presses or up presses.
However in my case you can click on elements within the business page to launch another fragment (leaving a stack count of 3), meaning I cannot 'turn on' tabs within onPause(), and found the best solution was to do it within onDetach(). This should mean the tabs are only turned on when the detail fragment is actually popped from the stack.
Hopefully this solution is accurate, I have toyed with many ideas including back stack listeners and overriding onBackPressed, but for me this seems to be the best option.
I'm creating the 3 tabs dynamically based on example here http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
the application works well except one issue.
when I launch app and then click the Back Button then the app closes.
I launched the app by clicking on the application icon.
now I can see 6 tabs. each time I repeat this process, it keeps on adding 3 tabs.
when ever I use Menu button instead of back Button then it doesn't add 3 tabs.
anything going wrong here, how can I fix it?
thanks
Are you creating the tabs at onResume() or onStart()? Try to create them at onCreate()
or delete the tabs by onPause with something like:
LinearLayout tabbar = (LinearLayout) findViewById(R.id.tabbar);
tabbar.removeAllViews();
I am creating an app for Android 3.0+ that uses the action bar with tabs for navigation. I have 2 tabs that each load a ListFragment:
Tab 1 Tab2
A B
C D
Where A & C are list items in Tab 1 and B & D are list items in Tab 2.
Currently if you select Tab2 and then item B a new ListFragment is loaded on top of the current list fragment in Tab2. Pressing back takes you back to the original Tab2 contents. However if you don't press back and instead select Tab1 you see Tab1's contents on top of the new content you loaded in Tab 2. So I am guessing there is a better way of doing this. I wanted to use multiple fragments in each Tab to reduce the code complexity in each tab (for instance the onListItemSelected handling). Is there another way to do that without using multiple fragments? I am also trying to just save state but I am not sure how to tell the difference from when the user clicks back (then I would want to restore state) and the user has selected tab 1 and then selects tab 2 again (i would want to restore state).
Thanks
I am having similar struggles with the complexity of fragments and tabs on the Action bar. It seems to me that android for tablets is still very much in its infancy and there are various issues (like yours and the issues I'm having) that will be addressed in the coming months. For now, I think you just have to handle some of those complexities yourself instead of relying on the Android framework.
To answer your first question more directly, I think you need to step back and think out the functionality before diving into the code. I can give you a potential solution (keep track of your fragments manually in the activity and simply add and remove fragment as necessary via the ActionBar.TabListener interface.
Also, if you want the user to be switching from one fragment to another like you are suggesting, perhaps the back functionality shouldn't be used. What happens if the user clicks on the tabs 10 times in a row. Do you want the back button to take the user back through all of those events? I think the back button should only be used when the navigation system is very sequential and "back" is an easy concept. Check out all the Google apps that use tabs to see what I'm talking about.
i've got a tabbed layout, and on one of the tabs i have a search functionality. When the user makes a new search, i need to show the results. However, doing so involves starting another activity to handle the search results.
this causes the tabs at the bottom to disappear. The user can get the tabs back by clicking on the 'back' button. But somehow, in the context of my application this can be a bit counter-intuitive and seems to be break the common layout flow.
is there any way to prevent the tabs from disappearing when invoking the search from one of the tabs?
thanks for any help/suggestions.
as far as i can understand your problem, You are not using tabs then you are using buttons. see some tabhost tutorials on how to create a tabbed activity. what you are doing is launching a new activity instead of just switching a tab in the existing one.
Also the other things you can look for are activity groups.
Hope this helps you somehow.
I have created a TabActivity in my android application. When user selects the second tab, an alert dialog will appear. Is there any way to navigate to the previous tab when user clicks the cancel button for the dialog box.
regards
dj
I found the solution here:
Android - Switch Tabs from within an Activity within a tab
In my case, I have used the solution of 'GrandPrix'.
I think you need to first know about the Tab Widget:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
http://www.androidpeople.com/android-tabhost-tutorial-part-1/
You can set the tab using setCurrentTab method:
tab_host.setCurrentTab(2); // 2nd tab button is showing initially
same way, you can set the tab-button on the cancel button.