I was playing with the activity life cycle today. I had logged all the lifecycle methods. I was on the MainActivity and then switched off the phone suddenly. I noticed 2 methods being called:
onPause,
onStop,
but onDestroy never got called.
Any take on this?
If by "switched off the phone" you mean you turned off the screen, your results are fairly common. There will be differences between devices, due to OS version and manufacturer changes, so you should not make any assumptions. However, I would not expect your activity to be destroyed.
If by "switched off the phone" you mean you completely powered down the phone, there is no guarantee that the OS will take the time to destroy all running components. Once again, there will be differences between devices, due to OS version and manufacturer changes, so you should not make any assumptions.
Is normal because:
onDestroy() is called before the activity is destroyed. The system invokes this callback either because:
the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)
https://developer.android.com/guide/components/activities/activity-lifecycle#ondestroy
Related
I know that it is a real problem with running apps in background for different OEMs, but how can I solve this problem?
My app has a webview integrated from an online radio, everything is ok, but after 5 minutes (in locked screen mode), the player stop playing... I can configure my mobile settings, going to Apps, Special access, Optimize battery usage, search for my app and disable that button, but I win only 10 minutes (15 in total), and the app will stop again...
I found something about services, but I'm a really beginner, I don't understand why should I use a service to run in foreground... I have also a notification icon (+title+message) which is showing on display even if the mobile screen is locked. For my understandings, that means the app is running in foreground and in background. Can't figure out how to solve this.
I'm a beginner, but I want to go with this app in production (Google Play), and I want to be useful, not to be just another app...
I hope somebody will have the patience to respond on my issue. Thank you!
(At least some advice, what should I do...)
You need services to run in the background when your application is not visible to the user. Android automatically kills some apps especially when they are resource intensive. This can also be done by some antivirus software, task cleaners, memory cleaning apps etc.
You need to build your application around these challenges because users will not be required to optimize their settings for your application to run.
This services can be triggered by some android activity lifecycles. When you lock your screen, some life cycle methods like the onPause() and the onStop() could be called in your applications by default. You need to handle these events.
Services
A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. You need to create a service that will perform the tasks you want and periodically update the call back in your application.
E.g. The app may fetch notifications from a remote backend and periodically show them to the user at the notifications panel.
Android activity life cycles
As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity.
References
Android Services -> Learn about android services
Activity Life Cycles -> Learn about activity life cycles
My applications has multiple activities. They work perfectly fine until you decide to open another activity and go back to the last activity and repeat this process. Basically if you go from activity A to B, than go back to A and repeat this the app starts to lag. More specifically the activity A starts to lag until it is refresh. I have checked with leak canary for memory leak but it doesn't seem to be the case. Any clue about why is this happening?
Whats is the alternate method to find whether the app is killed from recent list or not in android Oreo?
Requirement : App kill state has to be saved in local db.
Is there any alternate method available or any libraries to monitor app activity killed from recents?
Requesting for any suggestions related onTaskRemoved in Oreo.
I don't think there's any event callback that instructs that your app is destroyed, if so, then it will re-alive that app - which is totally negating the idea of killing staled apps.
Instead, you can always save your app's state whenever user switches away from your app i.e. save whatever you want in onStop method of your Activity.
I'm developing an opengl ES 2.0 live wallpaper. Normaly, when an engine is destroyed, its methods onSurfaceDestroyed and onDestroy were called. I notice that when i click on 'Set wallpaper' button, onSurfaceDestroyed and onDestroy are not called.
This methods are usefull to destroy opengl ES contexts. Every time wallpaper switch between normal and preview mode the wallpaper crash due to the resource leak. Is anybody had the same problem?
I'm pretty sure I've figured it out for anyone else having this problem.
It seems that when I already have wallpaper running, then I click 'Set wallpaper' on its preview, the onDestroy() method isn't called. However, nullifying Bitmaps and calling stopSelf() can be put in the onSurfaceDestroyed method, this seems to work for each instance of the WallpaperService Engine open. It might not be the correct way to do things but it seems to work ok at the moment.
I have been doing my research, but I feel as if I am missing something.
I have an app with a login. Each time you open the app, you should be forced through that login page. You should never be able to resume onto any activity other than the login.
In the manifest I have
android:clearTaskOnLaunch="true"
on the main activity I wish to use as the login activity,
and
android:finishOnTaskLaunch="true"
as well as
android:excludeFromRecents="true"
on the rest of the activities.
The problamatic situation occurs when you go from the login to another activity, hit home, and relaunch the app via the icon. It should jump back to the login page, but it doesnt. Any idea?
I have also been installing as a regular apk, not via eclipse as I know that there is an issue with eclipse and some of the manifest attributes.
Perhaps if there is a way to detect that the activity launch came from the app icon press, I could manage it that way, but I dont think that is possible either.
In either onResume or onRestart you could check a series of flags, such as a login timeout, then force the user back to the login activity using an Intent, while at the same time finishing the original activity.
I like this method in favor or just finishing the app in either onPause or onStop because it gives you a chance to make some checks before blindly closing the application.
Or, you could try using the android:noHistory tag in your manifest file.
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
There are also other tags such as, android:finishOnTaskLaunch
Whether or not an existing instance of the activity should be shut down (finished) whenever the user again launches its task (chooses the task on the home screen) — "true" if it should be shut down, and "false" if not. The default value is "false".
More information here: http://developer.android.com/guide/topics/manifest/activity-element.html
The easiest and fastest way is probably to terminate the activity with finish(); in its onPause() since this is invoked when the App is put into the background.
It might be possible to solve with XML configuration as well, but not that I know of by heart.