Java 如何在 Android Studio 的 ActionBar 上添加后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34110565/
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
How to add back button on ActionBar in Android Studio?
提问by Craig Gallagher
I have created an app and wanted a back button on my action bar to navigate back to the previous page using Android Studio. I have looked at a number of examples but keep getting errors under setDisplayHomeAsUpEnabled
我创建了一个应用程序,并希望我的操作栏上有一个后退按钮,以便使用 Android Studio 导航回上一页。我查看了许多示例,但在下面不断出现错误setDisplayHomeAsUpEnabled
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) {
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivityForResult(myIntent, 0);
// 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
采纳答案by adnbsr
Assuming that you have a DetailActivity and you need back button to MainActivity. First change your manifest to for DetailActivity
假设您有一个 DetailActivity 并且您需要返回按钮到 MainActivity。首先将您的清单更改为 DetailActivity
<activity
android:name=".DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity"/>
</activity>
and in onCreateof DetailActivity
并在onCreateDetailActivity 中
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
will fix things. This is the simplest implementation.
会解决问题。这是最简单的实现。
回答by Michele Lacorte
Use this in onCreate()
使用这个 onCreate()
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
Than add this listener in your MainActivity.javaclass
比在你的MainActivity.java班级中添加这个监听器
protected OnBackPressedListener onBackPressedListener;
public interface OnBackPressedListener {
void doBack();
}
public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
this.onBackPressedListener = onBackPressedListener;
}
@Override
public void onBackPressed() {
if (onBackPressedListener != null)
onBackPressedListener.doBack();
else
super.onBackPressed();
}
So now in your Fragment class you can implements MainActivity.OnBackPressedListenerand than:
所以现在在你的 Fragment 类中你可以实现,MainActivity.OnBackPressedListener然后:
@Override
public void doBack() {
//Do on back pressed operation
}
回答by Paresh P.
Make your activity to extends AppCompatACtivityand then set navigationIconto your toolbaralong with clicklistener
让你的活动扩展AppCompatACtivity,然后设置navigationIcon你toolbar一起clicklistener
For example
例如
public class MyActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_back_arrow); // Set the icon
// Icon click listener
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("cek", "home selected");
}
});
}
}
Make sure your activity theme is AppCompat
确保您的活动主题是 AppCompat
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customization will goes here -->
</style>
回答by Raghib Arshi
//In Kotlin
// go to AndroidManifest.xml file apply this change
<activity android:name=".PresentActivity">
// for adding back button on top
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".BackActivity"/>
</activity>
// Hope this will work.

