[英]How to navigate back to main activity without a button
I'm accustomed to using an intent to launch a new activity when a button is pressed. 我习惯于在按下按钮时使用意图来启动新活动。
But the app that I'm making uses a list view, which means I want to be able to go back from my searching activity to the main activity by using the phone's back button. 但是我正在制作的应用程序使用列表视图,这意味着我希望能够通过使用手机的后退按钮从搜索活动返回到主要活动。
I was reading and experimenting with different types of android methods, this one in particular which seemed simple but doesn't work, or perhaps I'm doing something completely different. 我正在阅读并尝试使用不同类型的android方法,尤其是这个方法似乎很简单,但是不起作用,或者我正在做的事情完全不同。
public void onBackPressed()
{
Intent setIntent = new Intent(this,MainActivity.class);
startActivity(setIntent);
finish();
}
Doesn't Android's onBackPressed method respond to any android phone's back button? Android的onBackPressed方法不响应任何Android手机的后退按钮吗?
Thank you for your help. 谢谢您的帮助。
You forgot to call super.onBackPressed() inside onBackPressed() method, you dont need to start a new intent to go back. 您忘记了在onBackPressed()方法中调用super.onBackPressed(),而无需启动新的返回意图。
In your search activity override the onBackPressed() method and call it from wherever you want in the activity. 在您的搜索活动中,覆盖onBackPressed()方法,然后从活动中的任意位置调用它。
it should be like this. 应该是这样
@Override
public void onBackPressed(){
super.onBackPressed();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.