Reputation: 783
I am using Android's native ActionBar (not sherlock) for a project. I have 1 FragmentActivity and 3 fragments: 2 of those ListFragments and the other a Fragment.
In my initial "homepage" fragment, I am setting a custom view (a spinner) in my app's ActionBar with this in my homepage fragment's onActivityCreated():
ActionBar actionBar = getActivity().getActionBar();
actionBar.setCustomView(R.layout.homepage_spinner);
Spinner spinner = (Spinner) actionBar.getCustomView().findViewById(R.id.homepage_selection_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.homepage_selection_spinner_text,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(mSpinnerListener.getCurrentPosition());
spinner.setOnItemSelectedListener(mSpinnerListener);
Then in onStart():
getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(false);
setHasOptionsMenu(true);
Everything in the homepage fragment works great. No problems at all. When I switch to my next fragment, I use this to try to get rid of the custom view (spinner) and display the up button, home, and the title of my app in onStart():
getActivity().getActionBar().setDisplayShowCustomEnabled(false);
getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
setHasOptionsMenu(true);
When I go to this fragment initially, it shows home, up button, but not the title. Then when I go to the last fragment of the app, same exact result. BUT when I rotate phone on these fragments, then it shows the title just like I was hoping. I know this is happening because the Activity is being recreated so the actionbar's custom view is being erased when I rotate phone. I have confirmed this with tests running getActionBar().getCustomView(); and checked when it is null and not.
Notes:
- my homepage fragment has setRetainInstance(true) in the onCreate().
- I have tried: getActionBar().setCustomView(null); with no luck
- tried getActionBar().setDisplayShowCustomEnabled(false);, setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);, setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); all with same effect
Upvotes: 4
Views: 2901
Reputation: 159
You can get back title and home icon by
getActivity().getActionBar().setTitle("Title");
getActivity().getActionBar().setDisplayShowHomeEnabled(true);
getActivity().getActionBar().setDisplayShowTitleEnabled(true);
getActivity().getActionBar().setDisplayShowCustomEnabled(false);
Upvotes: 0
Reputation: 783
After doing more testing and observing how Android behaves with different ActionBar scenarios, I have found a working solution. Kind of hacky, but it is the only way that I have found to work. It actually has nothing to do with the custom view...
Android seems to base the ActionBar on the fragment that it sets when the Activity is created. Let me explain with an example: Lets say we have 1 activity and 2 fragments. Fragment "A" has these options set:
getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
Fragment "B" has these options set:
getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
When the app first starts up and fragment A appears, there is no title. This is fine as I did not include ActionBar.DISPLAY_SHOW_TITLE for fragment A. When I go to fragment B, it also does not show title. This is what the whole issue here. Fragment B said to display the title, but it is not.
But if you rotate the phone when fragment B is focused, title shows up. Go back to fragment A then back to fragment B, title still shows up. Weird...
If you rotate the phone when fragment B is focused, title shows up. Go back to fragment A then back to fragment B, title still shows up. So everything is working great, go back to fragment A, rotate phone, and we are back to original problem just like the app started up for the first time. So it seems that Android follows the "rules" of displaying on the ActionBar on the fragment that is focused when the activity is created.
This is my solution. Works just like I intended:
in fragment A:
getActivity().getActionBar().setTitle("");
getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_TITLE);
Then in fragment B:
getActivity().getActionBar().setTitle(R.string.app_name);
getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
Upvotes: 2
Reputation: 50558
Have you tried getActionBar().getCustomView().setVisibility(View.GONE);
?
At this point the custom view is not null, now set it to null. :)
Your custom view will not be visible nor it can receive touch events.
Upvotes: 1