问题描述
在从AppCompatActivity扩展的MainActivity中,我想像这样重写onBackPressed方法:
in my MainActivity, which extends from AppCompatActivity, I want to override the onBackPressed method like so:
@Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}
,但不会调用onBackPressed.如果我不重写onBackPressed,该应用程序将关闭,当我按下后退按钮时,应用程序将关闭,如果我重写了它,则不会.
but onBackPressed does not get called. How ever if I do not override onBackPressed, the application closes, when I press the backbutton and if I do override it it doesn't.
其余的活动如下:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Drawer drawer;
private FloatingActionButton fab_test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab_test = (FloatingActionButton) findViewById(R.id.fab_test);
fab_test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"FAB Test pressed",Toast.LENGTH_SHORT).show();
}
});
buildDrawer();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer,page).commit();
}
@Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(getApplicationContext(),"onBackPressed",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
}
我说的是硬件后退按钮(不是操作栏上的按钮)
I'm talking about the hardware-backbutton(not the actionbar one)
推荐答案
该问题已得到解答,但是我觉得需要清除本主题中的某些内容.大多数评论和答案指出使用super.onBackPressed()
,这是导致方法onBackPressed()
无法使用的原因.但这是不正确的,重要的是让其他初学者知道.方法onBackPressed()
不需要使用super.onBackPressed()
.如果有人将super.onBackPressed()
注释掉,onBackPressed()
也可以使用.
This question is already answered, but I feel to clear something here in this topic. Most comments and answeres point out to use super.onBackPressed()
and that this is the cause of the not working method onBackPressed()
. But that is not correct and important to let other beginners know. The method onBackPressed()
does not need to use super.onBackPressed()
. onBackPressed()
also works if somebody, for example, comment super.onBackPressed()
out.
正如提问者所写,他不会使用super.onBackPressed()
,因为它将关闭活动.因此,其无法正常工作的原因可以分为三个可能的原因:
As the questionier has written, he won´t use super.onBackPressed()
because it will close the activity. So, the cause of this why it isn´t working, could be seperated into three possible causes:
- 由于logcat控制台中的过滤器错误,日志无法正常工作
- 由于错误的传递上下文,Toast不起作用
- 供应商错误地实施了操作系统.
通常,吐司通过传递正确的上下文来起作用.对于发问者,只需传递this
即可.
Usually, the toast works by passing the correct context. In the case of questioner, simply passing this
.
@Override
public void onBackPressed() {
Log.d("MainActivity","onBackPressed");
Toast.makeText(this,"onBackPressed",Toast.LENGTH_SHORT).show();
}
对于日志,只需在logcat上设置正确的过滤器即可.
For the Log, simply set the correct filter on logcat.
我不在乎现在是否有人投降票,但对于其他初学者来说,必须明确指出,不得使用super.onBackPressed()
.
I don´t care if somebody give downvotes now, but it must be clear for other beginners, that super.onBackPressed()
must not be used.
无论如何,使用onKeyDown()
也是一种解决方案.
Anyway, the use of onKeyDown()
also is a solution.
这篇关于Android的onBackPressed()没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!