简体   繁体   English

在暂停状态下完成活动,但仍处于后退状态?

[英]Finish activity onPause but still be in backstack?

I'm trying to minimize memory usage in my app, and one of the things I'm doing is calling finish() in the onPause method (which I know is not the best way to do things). 我正在尝试最小化应用程序中的内存使用,我正在做的一件事是在onPause方法中调用finish()(我知道这不是最好的处理方法)。 For the most part, it seems to be working well, but when the user clicks the back button from the next activity, it logically skips over the finished activity and goes back further. 在大多数情况下,它似乎运行良好,但是当用户在下一个活动中单击“后退”按钮时,它在逻辑上会跳过已完成的活动,然后再返回。 Is it possible to have that activity in the back stack and just get recreated if the user presses back? 是否有可能将这种活动放在后退堆栈中,如果用户按回去就可以重新创建该活动?

No. This conclusion comes from the task and backstack documentation as well as the activity documentation and a general understanding of how a stack data structure works. 否。该结论来自于任务和后退文档以及活动文档以及对堆栈数据结构如何工作的一般理解。

A stack data strucure only has 2 possible operations push/put, which adds something to the collection, and pop, which removes it. 堆栈数据结构只有2个可能的操作push / put,这会向集合中添加内容,而pop会删除它。 Stacks folow a last in first out model, or LIFO, where by last thing added - in your case an activity - is the first thing removed when pop is called. 遵循后进先出模型或LIFO进行堆栈,其中最后添加的东西(在您的情况下为活动)是调用pop时删除的第一件事。

Within the android lifecycle activities are generally popped from the stack when the back button is pressed. 当按下后退按钮时,通常会从堆栈中弹出android生命周期内的活动。 At that point onDestroy() is called and the activity is removed (you can verify this by overriding the onDestroy() method and logging the results if you want to check). 此时,将调用onDestroy()并删除活动(您可以通过覆盖onDestroy()方法并记录结果(如果要检查)来验证这一点)。 Alternativly you can force onDestroy() to be called by calling finish() as you are. 或者,您可以直接通过调用finish()来强制调用onDestroy() Finishing an activity effectivly does the same thing as pressing back. 有效地完成一项活动与按回去的功能相同。 The activity is destroyed and must be recreated before it can be added to the stack. 该活动已销毁,必须先重新创建,然后才能将其添加到堆栈中。

For what you're trying to do the stack would have to incorporate some intermediate state in which an activity does not exist but rather something akin to a reference is held that, when moved to the top, would indicate that the corresponding activity should be recreated. 对于您要尝试执行的操作,堆栈将必须合并一些中间状态,在该状态下不存在活动,而是持有类似于引用的内容,当移至顶部时,这表明应该重新创建相应的活动。 Since this is not how the sack works - it only holds activities - that state cannont exist and so the result you are talking about is not possible. 由于这不是麻袋的工作方式-它仅容纳活动-状态无法存在,因此您所谈论的结果是不可能的。

Your Goal is to minimize memory usage,Just make use of activity life cycle , You can do this alternative(if you need) 您的目标是最大程度地减少内存使用量,只需利用活动生命周期 ,就可以选择这种方式(如果需要)

-Just leave onCreate() method blank.(only do setContentView(layout)) -只需将onCreate()方法留空。(仅对setContentView(layout)做)

-Override onResume() ; -重写onResume() ;

-whatever you were doing in onCreate just copy paste to onResume(). -无论您在onCreate中执行什么操作,只需将粘贴复制到onResume()。

-and In onPause() , Recycle your all bitmaps and set them to null(I think you are using Bitmaps thats why you are very cautious about it ). -然后在onPause()中回收所有位图并将它们设置为null(我认为您使用的是位图,这就是为什么您对此非常谨慎的原因)。 and remove your views. 删除您的视图。

Now what will happen, when you launch your new activity, onPause() would be called. 现在将发生什么,当您启动新活动时,将调用onPause() that will remove your all bitmap and views. 这将删除您的所有位图和视图。 and when you come back, onResume() will be call.(onCreate will not be called). 当您回来时,将调用onResume() 。(不会调用onCreate)。 and that will again initialize your view and bitmaps. 这将再次初始化您的视图和位图。

No, i don't think that is possible. 不,我认为不可能。 Once you finish the Activity it's gone. 完成活动后,它就消失了。 You could, however, implement and handle your own stack. 但是,您可以实现和处理自己的堆栈。 On back pressed, you would just start the closed Activity again. 按下后,您只需再次启动已关闭的活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM