简体   繁体   English

Android在onPause()中使用finish()销毁活动

[英]Android destroy activity with finish() in onPause()

I dont think putting finish() in onPause() is cutting it. 我不认为在onPause()中放置finish()会削减它。

I have a Location activity and it is proving very hard to test, what I would like to do is be able to, when I leave the activity, completely destroy/kill any existence of it. 我有一个Location活动,并且很难测试,我想做的是,当我离开活动时,能够完全销毁/杀死它的任何存在。 So that when I go back, both when I leave the app or just the activity, everything is new, there is no cache, nothing and it has to start all over again. 因此,当我回去时,无论是当我离开应用程序还是只是活动时,一切都是新的,没有缓存,什么都没有,它必须重新开始。

The reason being is I don't want it to remember Last Know Locations, they are not useful in this app, all I want is the current location and if it can't be found, it can't be found. 原因是我不想让它记住“最后知道的位置”,它们在此应用中没有用,我想要的只是当前位置,如果找不到,就找不到它。

Cheers, 干杯,

Mike. 麦克风。

This gets into some deep discussions about how android manages activities and memory and all kinds of things which you probably don't want to think about. 这进入了一些关于android如何管理活动和内存以及你可能不想考虑的各种事情的深入讨论。 If you are currently trying to finish the activity in your onPause method (sounds like a horrible idea to me since your phone will call this whenver your screen goes to sleep -- after about 15 seconds of inactivity depending on your settings). 如果您当前正在尝试使用onPause方法完成活动(听起来对我来说是一个可怕的想法,因为当您的屏幕进入睡眠状态时,您的手机会调用此信息 - 根据您的设置,大约15秒不活动后)。 But if that really is what you want to do then why not just make the call to get the last known location in your onResume method? 但是,如果这真的是你想要做的,那么为什么不调用才能获得onResume方法中的最后一个已知位置?

将完成放入onPause并在清单中为此活动android:stateNotNeeded="true"因此它想记住该活动重新启动时的最后一个状态

为了解决此问题,您可以使用首选项来存储数据,也可以将其用于将来。

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

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