如何在机器人中覆盖操作栏后退按钮?

我想自定义的活动后退按钮在行动栏,而不是在硬键后退按钮。我已经重写了 onBackPressed()方法。它与我的模拟器后退按钮工作,但不与行动栏后退按钮。

我希望它发生在动作酒吧。我怎么能这样做呢?

这是我的代码:

@Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}

我已经使用了这个祝酒词,不管向后按是否工作,但实际的实现更改喜欢回到以前的活动。但是这并不适用于操作栏顶部的按钮(除了活动的标题)。

请任何人可以指定我的问题。

89176 次浏览

I think you want to override the click operation of home button. You can override this functionality like this in your activity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
        @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}

If you want ActionBar back button behave same way as hardware back button:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}

Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}


@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}

This will take the user to the intent pressing either key or the action bar button.

If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}

EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}


return super.onOptionsItemSelected(item);
}
///////////////////
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

I have achieved this, by using simply two steps,

Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"

example :

 <activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />

Step 2: in ActivityDetail add your action for previous page/activity

example :

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

There are several ways how to set up back button in bar:

1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home

2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.

.. for those who wonder why it doesn't work for them...

Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}

but this failed to catch the "Back" button press.

Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)

@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}

and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.

(1) Add Parent activity for your child activity (AndroidManifest.xml)

<activity
android:name=".ParentActivity" />

(2) override the onSupportNavigateUp method inside the child activity

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}