Let's talk about my usage scenario:
I wrote an activity that used the Actionbar.
In this activity, there are fragment, the default first open a homefragment, click on a button will enter Detailfragment.
When you start detailfragment, the problem arises:
I hope that when Detailfragment is turned on, Actionbar appears with the arrow "<" returned.
So I wrote in the OnStart:
@Override Public void OnStart () { super. OnStart (); = getactivity (). Getactionbar (); Actionbar.setdisplayhomeasupenabled (true); Actionbar.seticon ( new colordrawable (Getresources (). GetColor (Android. (r.color.transparent))); Here, I changed the icon to a transparent color. }
Actionbar on the back button, I want to click the button to close myself this fragment
So I listen to the onoptionsitemselected event.
@Override Public Boolean onoptionsitemselected (MenuItem item) { if (item.getitemid () = = Android. R.id.home) { getactivity (). Getfragmentmanager (). Popbackstack (); return false ; } return Super . onoptionsitemselected (item); }
However, when you click the Back button, you will not be able to work. You will find that onoptionsitemselected will not be called.
Workaround: call Sethasoptionsmenu at OnCreate (true);
@Override publicvoid onCreate (Bundle savedinstancestate) { Super. OnCreate (savedinstancestate); Sethasoptionsmenu (true); }
Handling the Actionbar icon State of Homefragment
Since I have a homefragment, this will be my Lord fragment, and I hope that when this homefragment is overwritten, by covering his fragment control Actionbar (for example, it looks like a return icon), When the fragment is closed and the homefragment appears again, it will be the same (with the icon, no return button).
To achieve this, it is necessary to get the icon of Actionbar, get it.
The code is as follows:
Drawable Actionbar_lasthomeicon;
@Override Public voidOnStart () {//re-display, then set iconSuper. OnStart (); ActionBar ActionBar=getactivity (). Getactionbar (); Actionbar.setdisplayhomeasupenabled (false); if(Actionbar_lasthomeicon! =NULL) Actionbar.seticon (Actionbar_lasthomeicon);} @Override Public voidOnPause () {//is overwritten, the icon is recordedSuper. OnPause (); ImageView ImageView=(ImageView) getactivity (). Findviewbyid (Android. R.id.home); Actionbar_lasthomeicon=imageview.getdrawable (); }
Reference:
Http://stackoverflow.com/questions/23349630/actionbar-get-current-icon-findviewbyid
Android Development (fragment) is used in combination with Actionbar. Resolves an issue that does not trigger onoptionsitemselected, obtaining the default get icon for Actionbar