Android Development (fragment) is used in combination with Actionbar. Resolves an issue that does not trigger onoptionsitemselected, obtaining the default get icon for Actionbar

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.