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!