如何在应用栏的右上角箭头上实现类似设备上的返回按钮功能

huangapple go评论73阅读模式
英文:

How to get Back button(on device) like functionality on app bar back arrow on top right corner

问题

我有一个按钮,可以将我带回主屏幕。但每次我点击该按钮时,都会加载屏幕。但当我按下手机上由Android提供的返回按钮时,页面就不会一遍又一遍地加载。我不希望我的活动被重复加载,我想要与返回按钮相同的功能。

我已经尝试过

Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

但这会一遍又一遍地加载整个活动。任何建议都很有价值。主屏幕上有一个地图,每次我点击那个按钮时都会加载。

英文:

I have a button which takes me back to the home screen. But this loads the screen everytime i click on that button. But when I press the back button(given by android on phone) then it doesn't load the page again and again. I don't want that my activity is loaded again and I want to get the same functionality as the back button.
I have tried

Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

But this loads the whole activity again and again.
Any suggestions are valuable. The main screen has a map which gets loaded evertime I hit that button.

答案1

得分: 1

你需要理解两个概念 -

  1. 任务和返回栈
  2. 启动模式

任务和返回栈

任务是用户在执行某项任务时与之交互的活动集合。这些活动按照它们被打开的顺序排列在一个栈中(即返回栈)。简单来说,返回栈就是一个活动的栈,当用户以先进后出(FILO)的方式启动新活动时,该栈会增长。

如果用户按下设备上的返回按钮,那么新的活动将被完成并从栈中弹出,这将导致栈上的上一个活动调用 onResume()

另一方面,

如果你在一个活动上调用 finish() 并手动使用 new Intent() 启动一个新的活动,那么在栈上,新的活动将位于顶部并启动其自己的活动生命周期

启动模式

启动模式只是用于告诉Android如何启动相应活动的标志。

android:launchMode=["standard" | "singleTop" | "singleTask" | "singleInstance"]

standard: 默认值。系统总是在目标任务中创建活动的新实例并将意图路由到该实例。

singleTop: 如果目标任务的顶部已经存在该活动的实例,则系统将通过调用其 onNewIntent() 方法将意图路由到该实例,而不是创建该活动的新实例。

了解更多关于任务和返回栈活动标签的信息。


现在回到你的问题,如果你将标志 android:launchMode="singleTop" 添加到 AndroidManifest.xml 中的主屏幕活动标签如下 -

<activity
...
android:launchMode="singleTop">

那么活动将不会被重新创建,你将不会再次加载屏幕。在Android中有更高效的导航方式,显然这不是最佳方式。使用片段和导航架构可用于使生活更加轻松。

英文:

You need to understand two concepts here -

  1. Task and Back Stack
  2. LaunchMode

Task and Back Stack:

A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack—the back stack)—in the order in which each activity is opened. In simple word back stack is just an stack of activity which grown as user start launching new activities in FILO (First in Last Out) manner.

> If the user presses the Back(on device) button, that new activity
> is finished and popped off the stack, which will cause last activity
> on stack to call onResume().

on Another hand

> If you are calling finish() on an activity and manually starting a new
> activity with new Intent() then on stack, new activity will be on
> top and will start its own activity life-cycle.

LaunchMode

Launch mode are nothing but flags to show android that how the corresponding activity should start.

android:launchMode=[&quot;standard&quot; | &quot;singleTop&quot; |&quot;singleTask&quot; | &quot;singleInstance&quot;]

standard: Default. The system always creates a new instance of the activity in the target task and routes the intent to it.

singleTop:
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

A good read on Task and Back Stack and Activity Tag


Now coming back to your question, if you will add flag android:launchMode=&quot;singleTop&quot; to your home screen activity tag under AndroidManifest.xml as follows -

&lt;activity 
    ... 
    android:launchMode=&quot;singleTop&quot;&gt;
&lt;/activity&gt;

then activity will not be recreated and you will not get screen loading again. There are more efficient way to navigation in android and clearly this is not the best way. Using fragments and navigation architecture are available to make life easier.

huangapple
  • 本文由 发表于 2020年8月9日 16:21:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63324079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定