问题
I want to just add back button in Right side of action bar and I found many link for this.
This is my code which is app->res->menu->main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/backAction"
android:icon="@drawable/ic_launcher"
android:title="back"
android:showAsAction="always"/>
</menu>
Optionmenuactivity.java
Oncreate event:
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
onoptionItemselected()
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Manifest File :
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
Problem with this code:
Activity Flow is: (Main)Activity1-> Activity2 ->Activity3 ->Activity4
after Activity4, if we press backbutton, it should go on Activity3 but with this code it goes Mainactivity which is Activity1
Please help me ..Thank you
回答1:
First of all i wanted to say what you are trying to do is not a good practice, why would you want a back button in your action bar if Android devices already have one on the bottom of the screen and you can turn on Up-Navigation at the left side of the Action Bar?
Custom App-Navigation clutters your code, confuses users and most of the time is just ugly.
But if you really need a button that does the navigation, you want it to access the standard android navigation methods.
For Back Navigation:
Just use the standard android back action onBackPressed().
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
If this function doesn't do what you want, you can just override it with your own wanted behavior.
For Up Navigation:
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.backAction:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You will have to configure your parent activites in the manifest, but because of that the behavior of this Navigation is easier to change.
回答2:
There are a lot of problems here.
Firstly I recommend you, to understand the difference between Back and Up
Up navigation explained with examples Back navigation explained with examples
Secondly, it's not very easy to understand what's happening in your code, but as far as I see, you HAVE NOT declared in the manifest.xml the parent elements.
This is a nice and simple example:
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
A parent activity definition can EASE your life. This way you can define which activity will be loaded on UP NAVIGATION interaction.
Hope that helps :)
来源:https://stackoverflow.com/questions/28232136/back-button-is-not-working-properly-in-right-side-of-action-bar