android 后退按钮,android - 在动作b上显示后退按钮

android - 在动作b上显示后退按钮

我正在尝试在Action bar上显示onCreate以移动上一页/活动或移动到主页面(首次打开)。我不能这样做。

我的代码。

ActionBar actionBar = getActionBar();

actionBar.setHomeButtonEnabled(true);

该代码在onCreate。

18个解决方案

177 votes

这是一个简单的显示后退按钮

actionBar.setDisplayHomeAsUpEnabled(true);

然后你可以在onOptionsItemSelected上自定义后台事件

case android.R.id.home:

this.finish();

return true;

fhamicode answered 2019-05-18T20:35:46Z

138 votes

我认为AppCompat是最好和最简单的方法,请查看下面的代码。

如果你想以编程方式添加此行在AppCompat方法中添加此行

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

注意:确保Actionbar不为null。

并覆盖此方法

@Override

public boolean onSupportNavigateUp(){

finish();

return true;

}

而已

或者以非编程方式,您可以将meta添加到清单文件中的活动

android:name="android.support.PARENT_ACTIVITY"

android:value="MainActivity" />

编辑:如果你没有使用AppCompat活动则不要使用support字,可以使用

getActionBar().setDisplayHomeAsUpEnabled(true); // In `OnCreate();`

@Override

public boolean onNavigateUp(){

finish();

return true;

}

感谢@atariguy的评论。

Inzimam Tariq IT answered 2019-05-18T20:36:55Z

62 votes

魔术发生在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);

}

}

Matthias Robbers answered 2019-05-18T20:37:20Z

43 votes

官方解决方案

将这两个代码段添加到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。

...

android:name="com.example.myfirstapp.MainActivity" ...>

...

android:name="com.example.myfirstapp.SubActivity"

android:label="@string/title_activity_display_message"

android:parentActivityName="com.example.myfirstapp.MainActivity" >

android:name="android.support.PARENT_ACTIVITY"

android:value="com.example.myfirstapp.MainActivity" />

参考这里:[http://developer.android.com/training/implementing-navigation/ancestral.html]

hoot answered 2019-05-18T20:38:04Z

30 votes

将这些行添加到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);

}

}

希望对你有帮助..!

Abhishek answered 2019-05-18T20:38:41Z

21 votes

尝试此代码,仅在需要后退按钮时才考虑它。

@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;

}

Jaime answered 2019-05-18T20:39:05Z

15 votes

在您的方法上添加:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

在中定义父活动(按下操作栏中的后退按钮后将调用的活动):

在Manifest上的定义中,添加以下行:

android:parentActivityName="com.example.activities.MyParentActivity"

Bruno Peres answered 2019-05-18T20:39:43Z

8 votes

我知道我有点迟了,但能够通过直接关注文档解决这个问题。

将元数据标记添加到MainActivity(因此系统知道)

android:name=".Sub"

android:label="Sub-Activity"

android:parentActivityName=".MainChooser"

android:theme="@style/AppTheme.NoActionBar">

android:name="android.support.PARENT_ACTIVITY"

android:value=".MainChooser" />

接下来,启用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开发者文档

Avi Parshan answered 2019-05-18T20:40:34Z

5 votes

我知道以上是很多有用的解决方案,但这次我读了这篇文章(当前的Android Studio 2.1.2与sdk 23),上面的一些方法不起作用。

以下是我的子活动解决方案是MapsActivity

首先,您需要添加parentActivity

AndroidManifest.xml中

像这样 :

...

android:name="com.example.myapp.MainActivity" ...>

...

.....

android:parentActivityName=".MainActivity" >

android:name="android.support.PARENT_ACTIVITY"

android:value="com.example.myapp.MainActivity" />

其次,确保您的子活动扩展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);

}

}

希望这会有所帮助!

postace answered 2019-05-18T20:41:35Z

4 votes

要实现这一目标,只需两个步骤,

第1步:转到ActivityDetail并在action标签中添加此参数 - android:parentActivityName=".home.HomeActivity"

例:

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);

}

Vivek Hande answered 2019-05-18T20:42:18Z

3 votes

在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文件 -

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

这就是我们所要做的。

PAWAN LAKHOTIA answered 2019-05-18T20:43:03Z

2 votes

试试这个,在你的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);

}

}

Melbourne Lopes answered 2019-05-18T20:43:35Z

2 votes

我以这种方式解决了

@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();

}

Ardit Zeza answered 2019-05-18T20:44:01Z

1 votes

我的工作代码返回屏幕。

@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);

}

Shihab Uddin answered 2019-05-18T20:44:27Z

1 votes

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;

}

pruthwiraj.kadam answered 2019-05-18T20:44:46Z

1 votes

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);

}

akshay shetty answered 2019-05-18T20:45:05Z

0 votes

回答可能为时已晚,但我认为我的解决方案更短,功能更强。

// 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;

}

ndungujan answered 2019-05-18T20:45:31Z

0 votes

在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);

}

}

Manthan Patel answered 2019-05-18T20:46:04Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值