gpt4 book ai didi

android - 我可以强制 Android 后退按钮在 Activity 堆栈中后退两步吗?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:16 26 4
gpt4 key购买 nike

我知道我可以重写 onKeyDown 方法,但我希望 Back 执行此操作,只需两次!

最佳答案

FirstActivity

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);

第二个 Activity

int REQUEST_CODE = 123
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivityForResult(intent, REQUEST_CODE);

(为了使这个教学法有更多代码用于下面的这个 Activity )

第三 Activity

@Override
public void onBackPressed() {
// Semi ugly way of supporting that back button takes us back two activites instead of the usual one.
setResultOkSoSecondActivityWontBeShown();
finish();
}

private void setResultOkSoSecondActivityWontBeShown() {
Intent intent = new Intent();
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK, intent);
}
}

SecondActivity(再次)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 123) {
if (resultCode == RESULT_OK) {
finish();
}
}
}

关于android - 我可以强制 Android 后退按钮在 Activity 堆栈中后退两步吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2591900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com