Android Learning notes Actionbar as back to the top level

Source: Internet
Author: User

First, add the return icon to Actionbar:


Code:
@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);   Setcontentview (r.layout.settings); Issendmsg=(Switch) Findviewbyid (r.id.issendmessage); Iscall=(Switch) Findviewbyid (R.id.iscall); Data=(Initdataapp) getapplication ();  Issendmsg.setchecked (Data.issendmsg ());  Iscall.setchecked (Data.iscall ()); ActionBar ActionBar=Getactionbar (); Actionbar.setdisplayhomeasupenabled (true); //Actionbar.setlistnavigationcallbacks (adapter, callback);  }

Set available

To override the Onoptionsitemselected method:
@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseAndroid. R.id.home://Navutils.navigateupfromsametask (this);//onsaveinstancestate//This was called when the Home (UP) button is pressed//In the Action Bar.Intent parentactivityintent=NewIntent ( This, Mainactivity.class); Parentactivityintent.addflags (Intent.flag_activity_clear_top|intent.flag_activity_new_task);                       StartActivity (parentactivityintent);        Finish (); //onbackpressed ();   return true; }  return Super. onoptionsitemselected (item);}

This method can implement processing when the call is returned up.

Receive: Android. R.id.home There are several ways to handle this android.r.id.home, first comparing it:
  @Override  public  Span style= "Color:rgb (0, 0, 255); >boolean   onoptionsitemselected (MenuItem item) { switch   (Item.getitemid ()) { case   Android. R.id.home:navutils.navigateupfromsametask ( // onsaveinstancestate    return  true   return  super  Span style= "Color:rgb (0, 0, 0); >.onoptionsitemselected (item); }

When the test is rolled back, there is a loss of data to the previous activity. (test mode, add a edittext in a, then enter the data, click to jump into the bactivity, the use of up-level navigation will show the loss of data)

@Override Public Booleanonoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) { CaseAndroid. R.id.home:intent parentactivityintent=NewIntent ( This, Mainactivity.class); Parentactivityintent.addflags (Intent.flag_activity_clear_top|intent.flag_activity_new_task);            StartActivity (parentactivityintent);   Finish (); return true; }  return Super. onoptionsitemselected (item);}

The data is lost using the same intent method.

Later I looked at the source code, found to click on the phone's fallback button, triggered onbackpressed (); method, this method is to return the time axis of the previous activity, you can save the data.
 Public Boolean onoptionsitemselected (MenuItem item) {  switch  (Item.getitemid ()) {  case  Android. R.id.home:   onbackpressed ();    return true ;  }   return Super . onoptionsitemselected (item);}

However, when testing, it was found that this method only returns the previous one, not the logical designation, for example: A->b->c, return from C to A, if using onbackpressed (), then go back to B, so wrong. To this we can only use Navutils.navigateupfromsametask (this);//onsaveinstancestate, but we want to save the data in real time.

Next we will be the preservation of data processing, on-site protection!

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.