Live wallpaper engine does not call onDestroy when switch off preview mode - java

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.

Related

Does Switching off the Android Phone destroys the activity?

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

How to resume activity in Android?

I am trying to create my own Alarm Android application. I want to achieve that when alarm is triggered, MainActivity is resumed (not created again). It basically means that if I set alarm and leave my application, I want that application to be resumed when alarm is triggered.
Currently, I am facing a problem that when alarm is triggered while my application runs in background and I click on application icon, onCreate method is called and basically two instances of application are running simultaneously (I have used Toast messages to confirm this). I expected that click on application icon will cause its resuming if it is already running in background, but it seems it is not the case.
Also, I have tried procedure explained here: Resume activity in Android but it didn't work for me, Toast message from onCreate method appears on screen.
Can anybody help, please? I am really running out of ideas here. Thanks in advance!
What you need to do is specify your activity's launch mode to singleTask or singleInstance. To do this, go to your AndroidManifest.xml and change/add launchMode to your activity.
<activity
android:name=".YourActivity"
android:label="Your Activity"
android:launchMode="singleInstance">
More info on the differences of different launch modes are explained here: https://developer.android.com/guide/topics/manifest/activity-element

Android Oreo onTaskRemoved event is not working (Android version Oreo)

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.

System.exit equivalent in Android?

I know that this call should not be made but at the moment is the only thing stopping the activity fast enough.
Basically after returning the app from the background (or using the DDMS "Terminate Application" button) some static variables are null. I would like to restart the activity in order to stop crashes and so all values are updated.
At the moment System.exit(0) has done what I want, but I know this is not good.
I have tried finish(), but that does not stop the activity fast enough, it keeps executing some instructions that still causes the crash.
Any suggestions on how to manage this problem.
Standard way to close your running android app is simple:
finish();
or more complicated way is this:
android.os.Process.killProcess(android.os.Process.myPid());
Only in Android api 21 > you can use >
getActivity().finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent
tasks list.
It stops the application as fast as System.exit (0). But is it better than System.exit(0)? I don't know...
------------------ EDIT ---------------------
In below question
How to quit android application programmatically?
you can see all kinds of ways to stop your application.
According to the docs for Activity.finish():
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
When you call System.exit(), you are actually terminating the JVM; by finishing the activity, you allow the Android JVM to do its cleanup.

How to check if Android application goes to foreground?

Is it possible to check if the Android application goes to foreground? Maybe anything callback function? Any idea or suggestions is appreciated.
You can write code for callback activity in onRestart() Method.
ActivityManager.getRunningTasks(1).get(0).yourActivity.getPackageName()
This method can get the foreground app package name.
you can use thread listen change.
As per android life cycle onResume and onStart will be called once your activity comes to foreground. For more details Android life cycle

Categories