Proper way to configure ActionBar when using Fragments - android

I have many fragments inside my activity. One fragment displays Top Books and when clicking on book this fragment is replaced by fragment showing details of clicked book.
Fragment showing top books should have no title in ActionBar, but drop down navigation with categories. However fragment with details of book should have title in ActionBar.
Here comes my question: how should I handle ActionBar changes in my application. Should I configure ActionBar inside Activity or inside Fragments? If in Activity how can I handle all those changes: back button pressing, up navigation button pressing?
Please help, this situation lacks of good examples.

I'd configure the ActionBar in the Activity.
You can override the onBackPressed() method to do different things depending on what fragment is showing.
This reference shows how to handle the up button being pressed, and you can change the logic depending on what fragment is showing as well: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

Related

Fragment Management - Navigation Drawer

I have a question about fragment management.
I have already programmed a drawer activity based on fragments. After every click on a menu item you will land on an empty fragment.
Now I want to have it so that no matter on which fragment I am on top the same is written. That's about 5 things that are there and depending on the action certain values on the fragment change whatever is there. Like a Information bar.
What options do I have and how do I do that?
Current project scope:
Main activity java + xml
and the Fragments + xml
The main activity is only for the Navigation Drawer. The app starts with a fragment.
How can I implement this?
in you main activity just commit which fragment you want to fill that first view with in onCreate.

Android Drawer Navigation with a ListView, need explanation

I have implemented a basic Navigation Drawer.
I understand Android Drawer Navigation utilizes Fragment (Nav1 opens Frag1, Nav3 opens Frag3).
I want to achieve Frag1 (or the Home Activity) as a list (i.e., ListFragment) And, when an object on the list is clicked it will take me to Frag4....Frag10 depending on what is clicked.
In another term,
Drawer Menu (Nav1...Nav3)
Home Activity (Could be Frag1) is a list and a swipe tab << This is where I am stuck.
Other Activities are Swipe tabs too...
Any suggestion, close example?
Thanks
OK, I have found the answer, a very simple solution. Create ListFragment class. And in the main class just receive the list!
I have taken help from ListFragment

Navigation drawer with fragments and only one activity

Im doing an app with navigation drawer.
For this, i have a HomeActivity, this contains all the login of my navigation drawer, the options in menu, the view, the titles etc. And here, i set listenerclick for navigation elements.
This listener receives FragmentManager and with a switch do:
smf.beginTransaction().add(R.id.frame_content, new Fragment()).commit();
Replacing fragment for the fragment that i need in each case of switch.
In home layout i have a framelayout and navigation drawer.
Mi question is, is correct that i only have one activity with a framelayout, and depends on the item clicked in navigation drawer i replace the fragment on the frame, or is better have lot of activitys, and create menu in all of them with the same login, and when user click in item menu, launch new intent with the activity selected?
I hope i have explained ok...
Thank you.
I did this same thing, but I found it was a lot better to have different activities.
If you do go down the separate activities path you should have one base activity that the activities extend so you don't need to rewrite the drawer code.
A fragment is only really meant to be an extension of an activity, for example when you have multiple tabs, or you are swiping between different views, or you need to break up your activity into sections.

Android changing setNavigationMode per fragment is crashing the app

I created the below project so you can see my exact code and what is going on:
https://github.com/CorradoDev/TabsTest/commit/8f054dab2371b791c4061ceb511413f720f65d67
Basically what I am trying to do is hide the tabs for some pages and show them in other pages.
Below is the code I am using to show the tabs in the onresume
if(getActivity().getActionBar().getNavigationMode()==ActionBar.NAVIGATION_MODE_STANDARD){
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
THen to hide the tabs I am doing the below on resume:
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
When I am on the first fragment(nothing in backstack). I can show and hide the tabs on hte second. It gives errors sometimes with changing tabs.
When I am on the second fragment in the backstack and I hide the third fragment. I see the second and third fragment both call the onrefresh but the third fragment does not show.
I am confused on what is going on and why this is not easier.
Below is the error I generally get
03-27 15:26:31.029: E/AndroidRuntime(5505): java.lang.IllegalStateException: Fragment already added: Fragment3{41f2e390 #2 id=0x1020002 fragment3}
I still would like to know why the above does not work. But my fix was to create another activity with the fragment and no tabs. That seems to work well. But I am interested if they did not intend you to change tabs and no tabs per fragment.
I had a similar situation - only that I used NAVIGATION_MODE_LIST instead of tabs. I run into similar issues when I called a fragment from another fragment e.g. click on a list item opening up the item details.
Now I call all fragments from the main activity which allows me to control the set up of the actionbar. Whenever the navigation list should disappear I just call NAVIGATION_MODE_STANDARD when the fragment is called and NAVIGATION_MODE_LIST for the other fragments.

Share ActionBar between different activities

I have an application with an FragmentActivity called MainActivity which holds two fragments, one menu fragment to the left and one content fragment to the right. On a large device both of these fragments are shown. On a small device only the menu fragment is shown and when the user presses an item in said menu the content fragment is displayed in a new Activity.
Lets say the user is currently using the application on his phone and only the menu is visible:
The ActionBar of MainActivity can be manipulated by user choices in the menu. When the user selects a specific menu item, a new Activity launches to display the content fragment. When this Activity pops up the ActionBar of this new Activity is a new ActionBar and therefore does not resemble the ActionBar of FragmentActivity (MainActivity).
I figured I could pass the ActionBar from MainActivity to the Activity which displays the content fragment through Intent extras, or making the ActionBar static and reference it like MainActivity.actionBar or similar, but I ran into problems very quickly :)
Any suggestions on how to solve this? Basically, what I want is to share a single ActionBar reference between all my activities.
Maybe it's better to use fragments instead of activities. I mean show menu fragment, and when you need to show content fragment just put content fragment into the place (container) where was menu fragment. I think better/easier to organize fragment replace/change logics than trying to share actionbar between different activities.

Categories

Resources