I have a quick question regarding a basic feature of Android. It's clear that onPause is called when the screen is partially visible. Further onStop is supposedly called only when the screen is no longer in either the foreground or background.
Strangely however when I press the square home button on my phone onStop is called each time in my activity. Even though the activity screen is partially visible I see a log after 0.5 - 1 second of showing in the background. This causes onRestart to be called when returning from a simple pause. Since I am attempting to show an ad here this is slightly problematic. I tested on my Samsung Galaxy A51 and Pixel 3 XL API 30 emulator with the same result.
Currently I am wondering why this method is called here and would like to fix my understanding. I could very well be missing something obvious and apologize if so (low memory condition?). Below are screenshots with the system log and activity lifecycle diagram. I show before any taps, with the log window after 1 tap and then the log window after returning and tapping a 2nd time.
What is this pause button? If you mean it power button or home button, there should never be a hardware button directly connected Activity's lifecycle onPause.
When you press a power button or home button, it goes lock screen or Recents screen, in either of these scenarios your app is certainly stopped (onPause then onStop) rather than just suspended (only onPause).
It seems that going directly onResume from onPause may be rather rare case.
onPause()
In Android 7.0 (API level 24) or higher, multiple apps run in multi-window mode. Because only one of the apps (windows) has focus at any time, the system pauses all of the other apps.
A new, semi-transparent activity (such as a dialog) opens. As long as the activity is still partially visible but not in focus, it remains paused.
About the latter scenario, you can find an answer on StackOverflow.
So, if your app isn't coded to cause a direct onPause to onResume lifecycle, your app should go onStop right after onPause as a normal behavior.
Related
Android 4+
We're developing a lock screen app (not a widget). The app listens for SCREEN_OFF, and generates the activity, so that when SCREEN_ON is received, the user sees the activity in front of the security features using FLAG_SHOW_WHEN_LOCKED.
We've found that on rare occasions around 1 in every 50 to 100 times, the activity appears behind the Home screen rather than in front of the lock screen. We cannot replicate the issue, and it doesn't appear to happen when connected for debugging (I've sat and locked/unlocked my phone and not seen it, then taken the cable out and it happens in the next few times).
The activity appears to display over the wallpaper, with the home screen icons over the top of the activity.
3 questions:
1) Is it possible to "listen" for when the home screen gets started so that we could just finish() the activity at that point?
2) Is it possible to ensure that the activity runs over the lock screen all the time?
3) How would you test this to try to replicate the fault? I cannot figure out why it does it at all and cannot replicate it.
TIA
I'm maintaining an Android app that controls an industrial process. It has a main Activity/screen which allows you to selects various industrial tasks, and each task has its own activity/screen.
The problem - if the user is in one of these other Activities and the droid goes to sleep (say because the worker takes a break or goes to lunch), when it wakes up again it's back in the main Activity!
This is hard to debug because to debug it I have to be connected to the USB and when it's attached to USB it doesn't go to sleep.
1. What Activity Lifecycle events are associated with it going to sleep? What does it actually mean to go to sleep - the display goes off but obviously other stuff is happening, too, because when it wakes up it's in a different activity.
2. Is there a way I can allow it to enter sleep mode even when attached to USB so I can set breakpoints and debug this properly?
Details:
Samsung Galaxy Player (basically Samsung phone without the phone) running 2.35.
Activity that's open when it goes to sleep uses the default Standard launch mode.
When android goes to sleep, the top most activity receives onPause, but it still remains as the top most activity in the activity stack, and therefore it will receive onResume and will be started when the device awakes.
The reason you see your main activity when the device wakes up is that there should be some code in your onPause which forces the top most activity to finish after onPause (similarly there might be some settings. For example, setting noHistory to true in AndroidManifest.xml for an activity forces the activity to shutdown itself when it receives onPause, and so when you go back, you will see Main Activity on top).
When i press power key in my android game, the activity's onPaused get called as expected, but onResume is also get called after that, which cause the game music playing with lock screen turned on. And the screen is not turned off as expected.
The screenOrientation is set to landscape, and configChanges="screenSize|fontScale|orientation|keyboardHidden|keyboard".
I've searched for hours on the google and android documentation, but got nothing. Hope someone can enlighten me.
Using my mom's phone with my app if i hit the home button and then navigate back to the app, it is calling onCreate again. If I understand correctly this should not be the case right?
I know this is happening because my webview loads and it should just open up the page that was already open. Especially since I have the launch mode set to singleInstance...
What are some reasons to why it would be called twice on my mom's phone, but on my phone it works exactly how it should?
According to the Android documentation, your Activity can be killed at any time when it's not active which is the case when you switch back to the home screen.
If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process. When it is displayed again to the user, it must be completely
restarted and restored to its previous state.
I have been playing with the states all day trying to figure out why, when I press the power button to bring up the lock screen, my app loses focus and calls it's onStop() (as it should) but then it calls the onStart() again before hte screen goes off. This is causing me a problem because some sounds in my app (and presumably other stuff) start playing again while the lockscreen is active.
how can I make sure it is properly backgrounded and stopped when the lockscreen is active?
I faced this exact problem not long ago. In AndroidManifest.xml, make sure you have this:
android:configChanges="keyboardHidden|orientation"
This will prevent your activity from being restarted on runtime 'configuration changes'. See handling the configuration change yourself. That way your app will listen for events that would cause a restart - like orientation and keyboard visibility changes -- and handle them within your Activity.
There is also a very similar question on SO here:
Activity restart on rotation Android