按下“后退”按钮时如何终止活动?
我有一个活动,当它启动时,它会从互联网加载图像。为了节省内存,当按下后退按钮离开活动时,我希望活动转储所有数据,即删除其中的所有字符串和图像。我认为最好的方法就是终止该活动。
好吧,我似乎无法弄清楚按下“后退”按钮时的回调。因此,我一直在尝试使用 onPause()
和 onStop()
回调来执行任务,但这两种方法都会强制关闭我的应用程序。这是代码:
public void onPause() {
this.finish();
}
public void onStop() {
finish();
}
我尝试了多种变体,但似乎都不起作用。有什么想法吗?
I got an Activity that when it starts, it loads an image from the internet. In an effort to save memory, when the Activity is left by the back button being pressed, I want the activity to dump all data, that is get rid of all the strings and images that are in it. I figured the best way to do this was to just kill the activity.
Well, I can't seem to figure out the callback for when the Back button is pressed. So, I have been trying to use the onPause()
and the onStop()
callbacks for the task but both ways force close my app. Here is the code:
public void onPause() {
this.finish();
}
public void onStop() {
finish();
}
I've tried multiple variations of this, but none of them seemed to work. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单重写 onBackPressed 方法:
Simple Override onBackPressed Method:
将其添加到您的活动中
add this to your activity
我的应用程序用上面的代码关闭。
My app closed with above code.
首先,
finish()
不会破坏您的进程并释放内存。它只是从活动堆栈中删除该活动。您需要终止该进程,该进程在一堆问题中得到回答(因为这个问题被问了好几次)。但正确的答案是 - 不要这样做。Android 操作系统会在需要内存时自动释放内存。通过不释放内存,如果用户返回应用程序,您的应用程序将启动得更快。
请查看此处获取有关该主题的精彩文章。
First of all,
finish()
doesn't destroy your process and free up the memory. It just removes the activity from the activity stack. You'd need to kill the process, which is answered in a bunch of questions (since this is being asked several times).But the proper answer is - Don't do it. the Android OS will automatically free up memory when it needs memory. By not freeing up memory, your app will start up faster if the user gets back to it.
Please see here for a great write-up on the topic.
好吧,如果您研究应用程序生命周期的工作原理,此处 ,那么您就会知道,当另一个 Activity 获得焦点时,会调用
onPause()
,而当该 Activity 不再可见时,会调用onStop()
。据我所知,您只能从活动的和/或具有焦点的活动中调用finish()。如果您从
onPause()
方法调用finish()
,则意味着您在 Activity 不再处于活动状态时调用它。因此抛出异常。当您从
onStop()
调用finish()
时,活动将被发送到后台,因此将不再可见,然后出现此异常。当您按下
back
按钮时,onStop()
被调用。最有可能的是,Android 会自动为您做您当前想做的事情。
Well, if you study the structure of how the application life-cycle works,here , then you'll come to know that
onPause()
is called when another activity gains focus, andonStop()
is called when the activity is no longer visible.From what I have learned yet, you can call
finish()
only from the activity which is active and/or has the focus. If you're callingfinish()
from theonPause()
method that means you're calling it when the activity is no longer active. thus an exception is thrown.When you're calling
finish()
fromonStop()
then the activity is being sent to background, thus will no longer be visible, then this exception.When you press the
back
button,onStop()
is called.Most probably, Android will automatically do for you what you are currently wanting to do.