我试图在Back button
上显示Action bar
以移动上一页/活动或主页(第一次打开)。我不能这样做。
我的代码。
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
代码在onCreate
。
这是一个简单的显示后退按钮
actionBar.setDisplayHomeAsUpEnabled(true);
然后你可以在onOptionsItemSelected上自定义后台事件
case android.R.id.home:
this.finish();
return true;
要实现这一目标,只需两个步骤,
第1步:转到AndroidManifest.xml
并在<activity>
标签中添加此参数 - android:parentActivityName=".home.HomeActivity"
例:
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
第2步:在ActivityDetail
中添加你的action
用于上一页/活动
例:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
这很简单,对我很有用
在onCreate()方法中添加这个
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
在oncreate()方法之外添加这个
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
试试这个,在你的onCreate()
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
对于点击事件,
@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);
}
}
在onCreate方法写 -
Toolbar toolbar = findViewById(R.id.tool);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(ActivityOne.this, ActivityTwo.class));
finish();
}
这是xml文件 -
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:id="@+id/tool">
并在styles.xml中将其更改为
Theme.AppCompat.Light.NoActionBar
这就是我们所要做的。
我以这种方式解决了
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed(){
Intent backMainTest = new Intent(this,MainTest.class);
startActivity(backMainTest);
finish();
}
我的工作代码返回屏幕。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(), "Home Clicked",
Toast.LENGTH_LONG).show();
// go to previous activity
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
public void initToolbar(){
//this set back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//this is set custom image to back button
getSupportActionBar().setHomeAsUpIndicator(R.drawable.back_btn_image);
}
//this method call when you press back button
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
ActionBar actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
回答可能为时已晚,但我认为我的解决方案更短,功能更强。
// Inside your onCreate method, add these.
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
// After the method's closing bracket, add the following method exactly as it is and voiulla, a fully functional back arrow appears at the action bar
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
在oncreate();写这一行 - >
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
然后在该类中实现以下方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我认为onSupportNavigateUp()
是最好和最简单的方法,请检查以下步骤。步骤1是必要的,第二步有替代方案。
步骤1显示后退按钮:在onCreate()
方法中添加此行以显示后退按钮。
assert getSupportActionBar() != null; //null check
getSupportActionBar().setDisplayHomeAsUpEnabled(true); //show back button
第2步实现后退单击:覆盖此方法
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
多数民众赞成你完成了 或者步骤2备选:您可以将清单文件中的元素添加为
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
编辑:如果你没有使用AppCompat
Activity那么不要使用support
字,你可以使用
getActionBar().setDisplayHomeAsUpEnabled(true); // In `OnCreate();`
// And override this method
@Override
public boolean onNavigateUp(){
finish();
return true;
}
感谢@atariguy的评论。
的Manifest.xml
<activity
android:name=".Activity.SecondActivity"
android:label="Second Activity"
android:parentActivityName=".Activity.MainActivity"
android:screenOrientation="portrait"></activity>
在onCreate函数中添加以下代码:
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
然后覆盖:@Override public boolean onOptionsItemSelected(MenuItem item){onBackPressed();返回true; }
魔术发生在onOptionsItemSelected
。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
官方解决方案
将这两个代码段添加到SubActivity中
@Override
public void onCreate(Bundle savedInstanceState) {
...
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
添加元数据和parentActivity以显示以支持更低的sdk。
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.SubActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
参考这里:http://developer.android.com/training/implementing-navigation/ancestral.html
将这些行添加到onCreate()
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
并在onOptionsItemSelected中
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Write your logic here
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
希望对你有帮助..!
尝试此代码,仅在需要后退按钮时才考虑它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOUR CODE
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//YOUR CODE
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
在你的onCreate
方法上添加:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
在AndroidManifest.xml
中定义父活动(按下操作栏中的后退按钮后将调用的活动):
在Manifest上的<activity>
定义中,添加以下行:
android:parentActivityName="com.example.activities.MyParentActivity"
我知道我有点迟了,但能够通过直接跟随docs解决这个问题。
将元数据标记添加到AndroidManifest.xml
(因此系统知道)
<activity
android:name=".Sub"
android:label="Sub-Activity"
android:parentActivityName=".MainChooser"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainChooser" />
</activity>
接下来,启用MainActivity
中的后退(向上)按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_child);
// my_child_toolbar is defined in the layout file
Toolbar myChildToolbar =
(Toolbar) findViewById(R.id.my_child_toolbar);
setSupportActionBar(myChildToolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
}
而且,你将全部成立!
我知道以上是很多有用的解决方案,但这次我读了这篇文章(当前的Android Studio 2.1.2与sdk 23),上面的一些方法不起作用。
以下是我的子活动解决方案是MapsActivity
首先,您需要添加parentActivity
AndroidManifest.xml中
像这样 :
<application ... >
...
<!-- Main activity (which has no parent activity) -->
<activity
android:name="com.example.myapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
.....
android:parentActivityName=".MainActivity" >
<!-- Support Parent activity for Android 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myapp.MainActivity" />
</activity>
</application>
其次,确保您的子活动扩展AppCompatActivity
,而不是FragmentActivity。
第三,重写onOptionsItemSelected()
方法
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon action bar is clicked; go to parent activity
this.finish();
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
希望这会有所帮助!