android后退按钮图片_Android:后退按钮

android后退按钮图片

An introductory guide to back buttons in android apps.

android应用中后退按钮的入门指南。

目标: (Goals:)

  • make the little arrow appear in the app bar

    使小箭头出现在应用程序栏中
  • make pressing the little arrow take the user to a different activity

    按下小箭头可将用户带到其他活动
  • control what happens when the user presses their device’s physical back button

    控制用户按下设备的物理后退按钮时发生的情况

When I create an activity with the Android Studio Wizard, sometimes it includes a little arrow in the app bar, like this:

当我使用Android Studio向导创建活动时,有时它在应用栏中包含一个小箭头,如下所示:

Image for post
Location of the little arrow (aka ‘up button’)
小箭头的位置(又名“向上按钮”)

In the Android documentation, this is referred to as the ‘up button’ despite the fact that it points to the left.

在Android文档中,尽管它指向左侧,但仍被称为“向上按钮”。

To make the arrow appear we can include an android:parentActivityNameattribute to the activity’s entry in the manifest. The value of this attribute is the name of the activity that should be opened when the user taps the little arrow.

为了显示箭头,我们可以在清单中的活动条目中包含android:parentActivityName属性。 此属性的值是当用户点击小箭头时应打开的活动的名称。

<activity
    android:name=".Activity2"
    android:parentActivityName=".Activity1">
</activity>

This does not change the behaviour of the physical back button:

这不会更改物理后退按钮的行为:

Image for post
Location of a physical back button (varies from device to device)
物理后退按钮的位置(因设备而异)

Even when there is no little arrow at the top of the activity, the physical button is there.

即使活动顶部没有小箭头,物理按钮也在那里。

Pressing the physical back button takes the user back to the previous activity regardless of the what the manifest says. However, that might not always be what we want.

不管清单显示了什么,按下物理后退按钮都可以使用户返回上一个活动。 但是,这可能并不总是我们想要的。

For example, say you have a ‘loading’ activity that displays a spinner or progress bar while you are getting the next activity ready. When the user presses the back button, they are sent back to the loading activity, and have to press back a second time to get where they actually want to go. Even worse, if you implement logic to automatically progress to the next activity once loading is complete, the user would be bounced straight back to the next activity, which is even more confusing.

例如,假设您有一个“正在加载”活动,在准备下一个活动时显示微调框或进度条。 当用户按下“后退”按钮时,他们将被发送回加载活动,并且必须再次按下“后退”才能到达他们实际想要去的地方。 更糟糕的是,如果您实现了逻辑,以便在加载完成后自动前进到下一个活动,那么用户将直接跳回到下一个活动,这更加令人困惑。

i.e.

What we want:

我们想要什么:

Image for post

Default behaviour:

默认行为:

Image for post

To change this, we can override the onBackPressed function.

要更改此设置,我们可以覆盖onBackPressed函数。

In the above example, the default onBackPressed function would send the user back to the ‘loading’ activity. We can override the function to directly open Actvity 1.

在上面的示例中,默认的onBackPressed函数将使用户返回到“加载”活动。 我们可以覆盖该功能以直接打开Actvity 1。

class Activity2 : AppCompatActivity() {
    override fun onBackPressed() {
        val intent = Intent(this, Activity1::class.java)
        startActivity(intent)
    }
}

All done! Now the activity has a little arrow, it takes the user where they want to go, and the physical button behaves the same way.

全做完了! 现在,该活动有了一个小箭头,它将用户带到他们想要去的地方,并且物理按钮的行为方式相同。

翻译自: https://levelup.gitconnected.com/android-the-back-buttons-d71848a11b8a

android后退按钮图片

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值