Cannot get rid of custom view in ActionBar before changing screen orientation to fragments

I use native ActionBar (not sherlock) for Android for my project. I have 1 FragmentActivity and 3 snippets: 2 from these lists and the other is a Fragment.

In my original homepage snippet, I set up a custom spinner in my ActionBar application with this on my aboutActivityCreated () home snippet:

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 on the home page works great. No problem. When I switch to my next snippet, I use this to try to get rid of the user view (spinner) and display the up, home, and my app title 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 first go to this snippet, it shows the home button, up button, but not the title. Then, when I go to the last fragment of the application, the exact exact result. BUT, when I rotate the phone on these fragments, it shows the name as I hoped. I know this happens because activity is recreated, so the user actionbar view is erased when the phone is rotated. I confirmed this with tests run by getActionBar (). GetCustomView (); and is checked when it is zero, not.

Notes:
- on my home page there is setRetainInstance (true) in onCreate ().
- I tried: getActionBar (). SetCustomView (null); no luck
- tried getActionBar (). setDisplayShowCustomEnabled (false) ;, setDisplayOptions (ActionBar.DISPLAY_SHOW_TITLE) ;, setDisplayOptions (ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOWCUST all with the same effect

+4
source share
3 answers

After doing more testing and watching how Android behaves with various ActionBar scripts, I found a working solution. Kind of hacks, but this is the only way I found a job. This has nothing to do with the user view ...

Android seems to base the ActionBar on the fragment that it sets when the Activity is created. Let me explain with an example: Let's say we have 1 activity and 2 fragments. Fragment "A" has the following parameters:

 getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM); 

Fragment "B" has the following options:

 getActivity().getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE); 

When the application starts first and fragment A appears, there is no name. This is normal since I did not include ActionBar.DISPLAY_SHOW_TITLE for fragment A. When I go to fragment B, it also does not display the name. This is where the problem is. Fragment B said to display the name, but it is not.

But if you rotate the phone when fragment B is focused, the name appears. Go back to fragment A, then go back to fragment B, the name is still displayed. Weird ...

If you rotate the phone while focusing on B, a name appears. Go back to fragment A, then go back to fragment B, the name is still displayed. So everything works fine, go back to fragment A, turn the phone around, and we will return to the original problem, just like the application was launched for the first time. So, it seems that Android follows the β€œrules” for displaying on the ActionBar on the fragment that is focused when creating the activity.

This is my decision. It works as I expected:
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); 
+2
source

Have you tried getActionBar().getCustomView().setVisibility(View.GONE); ? At this point, the user view is not null, now it is null. :)

Your custom view will not be visible and will not be able to receive touch events.

+1
source

You can return the title and home icon.

 getActivity().getActionBar().setTitle("Title"); getActivity().getActionBar().setDisplayShowHomeEnabled(true); getActivity().getActionBar().setDisplayShowTitleEnabled(true); getActivity().getActionBar().setDisplayShowCustomEnabled(false); 
0
source

Source: https://habr.com/ru/post/1482580/


All Articles