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](https://miro.medium.com/max/9999/1*9k4tSAOHY7lPpvqB2aXkOw.png)
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:parentActivityName
attribute 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](https://miro.medium.com/max/9999/1*XoB1z6APSS8-ML3C9cOBaA.png)
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](https://miro.medium.com/max/9999/1*qCo3aeFuXrDt5OfCIjVCRg.png)
Default behaviour:
默认行为:
![Image for post](https://miro.medium.com/max/9999/1*nGaEAkts0wL9qpgFboOaVg.png)
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后退按钮图片