Android 将返回按钮添加到操作栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12070744/
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
Add back button to action bar
提问by haythem souissi
I have been trying to add a back button to the action bar.
我一直在尝试向操作栏添加后退按钮。
I want my view to look like this:
我希望我的视图看起来像这样:
I want to add the back button in the left of the action bar.
我想在操作栏的左侧添加后退按钮。
I added this code
我添加了这个代码
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
but it doesn't work.
但它不起作用。
How can I fix this?
我怎样才能解决这个问题?
回答by Danesh
After setting
actionBar.setHomeButtonEnabled(true);
设置后
actionBar.setHomeButtonEnabled(true);
Add the following code:
添加以下代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
回答by clami219
Add
添加
actionBar.setHomeButtonEnabled(true);
and then add the following
然后添加以下内容
@Override
public boolean onOptionsItemSelected(MenuItem menuItem)
{
switch (menuItem.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
As suggested by naXaI've added a check on the itemId
, to have it work correctly in case there are multiple buttons on the action bar.
正如naXa所建议的,我在 , 上添加了一个检查itemId
,以使其在操作栏上有多个按钮的情况下正常工作。
回答by Jason Saruulo
this one worked for me:
这个对我有用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ... other stuff
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.
当您使用 SupportActionBar 中的后退按钮时,将调用 onSupportNavigateUp() 方法。
回答by BoschAlex
After setting
设置后
actionBar.setHomeButtonEnabled(true);
You have to configure the parent activity in your AndroidManifest.xml
您必须在 AndroidManifest.xml 中配置父活动
<activity
android:name="com.example.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" />
<activity
android:name="com.example.SecondActivity"
android:theme="@style/Theme.AppCompat" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html
在这里查看更多信息http://developer.android.com/training/implementing-navigation/ancestral.html
回答by Von Iobro
There are two ways to approach this.
有两种方法可以解决这个问题。
Option 1: Update the Android ManifestIf the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar
选项 1:更新 Android Manifest如果设置 Activity 总是从同一个 Activity 调用,您可以在 Android Manifest 中建立关系。Android 会自动在 ActionBar 中显示“后退”按钮
<activity
android:name=".SettingsActivity"
android:label="Setting Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.example.MainActivity" />
</activity>
Option 2: Change a setting for the ActionBarIf you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).
选项 2:更改 ActionBar 的设置如果您不知道哪个 Activity 将调用设置 Activity,您可以像这样创建它。首先在扩展 ActionBarActivity 的活动中(确保您的 @imports 匹配您正在寻找的兼容性级别)。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_test);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
Then, detect the 'back button' press and tell Android to close the currently open Activity.
然后,检测“后退按钮”按下并告诉 Android 关闭当前打开的活动。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That should do it!
应该这样做!
回答by Arunendra
Firstly Use this:
首先使用这个:
ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
Then set operation of button click in onOptionsItemSelected
method
然后在onOptionsItemSelected
方法中设置按钮点击操作
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
回答by advantej
You'll need to check menuItem.getItemId()
against android.R.id.home
in the onOptionsItemSelected
method
你需要检查menuItem.getItemId()
针对android.R.id.home
的onOptionsItemSelected
方法
Duplicate of Android Sherlock ActionBar Up button
回答by M. Usman Khan
Simpler and better: For API >= 16
更简单更好:对于 API >= 16
Simply add "parentActivityName" for each activity in Manifest. The back button will automatically take u to the parent activity.
只需为 Manifest 中的每个活动添加“parentActivityName”。后退按钮将自动将您带到父活动。
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
回答by Amit Vaghela
Use this to show back button and move to previous activity,
使用它来显示后退按钮并移动到上一个活动,
final ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
回答by Saad Mahmud
if anyone else need the solution
如果其他人需要解决方案
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}