java Android 后退按钮不会返回上一个活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17643340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Back Button Doesn't Return to Previous Activity
提问by Dan Taylor
I have an app that has two activities: MainActivity and SettingsActivity. The MainActivity has a menu with a single Settings menu item. When this menu item is clicked, it launches the SettingsActivity with an intent. After the activity starts up, I click the back button in the top left corner and nothing happens. I assumed since I started the activity using an intent, the activity stack would be managed automatically. I want to return to the MainActivity. Am I wrong in this assumption?
我有一个有两个活动的应用程序:MainActivity 和 SettingsActivity。MainActivity 有一个带有单个设置菜单项的菜单。单击此菜单项时,它会以意图启动 SettingsActivity。活动启动后,我单击左上角的后退按钮,没有任何反应。我假设自从我使用意图启动活动以来,活动堆栈将被自动管理。我想回到 MainActivity。我在这个假设中错了吗?
MainActivity.onMenuItemSelected
MainActivity.onMenuItemSelected
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemID = item.getItemId();
if(itemID == R.id.settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
return true;
}
SettingsActivity
设置活动
public class SettingsActivity extends PreferenceActivity {
public static final String TEST = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
回答by Shobhit Puri
Inside your SettingsActivity
you need to override onOptionsItemSelected
to enable the back button on top left corner of Action Bar for going back. It does not know by itself that what it needs to do on click. Inside the case android.R.id.home
you can just call finish()
. This will finish your current activity and you will go back to MainActivity
which started it. Eg:
在您的内部,您SettingsActivity
需要覆盖onOptionsItemSelected
以启用操作栏左上角的后退按钮以返回。它自己不知道点击时需要做什么。在这种情况下,android.R.id.home
您只需调用finish()
. 这将完成您当前的活动,您将返回到MainActivity
开始它的活动。例如:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Just for completeness, to enable the home button, you may add the following in your onCreate()
of SettingsActivity
:
为了完整起见,要启用主页按钮,您可以在您的onCreate()
of 中添加以下内容SettingsActivity
:
getActionBar().setDisplayHomeAsUpEnabled(true);
As per docs of setDisplayHomeAsUpEnabled()
根据setDisplayHomeAsUpEnabled() 的文档
It is to show the user that selecting home will return one level up rather than to the top level of the app.
It is to show the user that selecting home will return one level up rather than to the top level of the app.
Hope this helps.
希望这可以帮助。
回答by Ismael ozil
Just add these lines in your OnCreate methord
只需在您的 OnCreate 方法中添加这些行
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
and override this method to add functionality to your back button:
并覆盖此方法以向后退按钮添加功能:
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} else if(id==android.R.id.home)
{
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Hope it helps you
希望对你有帮助