gpt4 book ai didi

android - 覆盖操作栏中的向上按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:26 26 4
gpt4 key购买 nike

我想避免在单击操作栏中的 < 按钮时破坏父 Activity 。

当我们按下那个按钮时,从 AppCompatActivity 调用了哪个方法?

我有办法覆盖它吗?有没有办法覆盖所有 Activity ?

最佳答案

i'd like to avoid parent activity being destroyed whenever i clicked < button in action bar.

一旦您按下该按钮,您的父 Activity 将暂停,而不是被销毁。

is there a way for me to override it? and is there a way to override for all activities?

只需调用:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

您需要将它调用到要覆盖按钮的 Activity 中。没有其他选择可以称之为“一劳永逸”。

which method is being called from AppCompatActivity when we press that button?

默认情况下,其中没有方法。如果您使用 Toolbar,请调用 setNavigationOnClickListener() 以在按下该按钮时执行某些操作。

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something here, such as start an Intent to the parent activity.
}
});

如果您使用 ActionBar,该按钮将在 Menu 上可用:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// do something here, such as start an Intent to the parent activity.
}
return super.onOptionsItemSelected(item);
}

关于android - 覆盖操作栏中的向上按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30679133/

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