Android changes activity when it goes to sleep - android

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).

Related

Android onStop gets called right after tap home button

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.

How can I turn on screen when my app running in background of a program?

I have a program which runs another program.
I want to prevent the user from pressing the power button to turn the screen off and if the user turned the screen off , my program turns it back on!
For this I implement a broadcast receiver that catches the screen off event, then it calls a service to run an activity and in this activity I will turn the screen on and that activity then runs my desired program ...
This plan works fine when I'm not running that app and the screen goes on when the user presses the power button! But when my desired application is running, my program does not work properly in the background! Sometimes it turns the screen on, sometimes it doesn't!

Android Lock Screen appears behind home screen occasionally

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

How to stop calling onCreate again when launching application in kindle fire device?

I developed an android application with two screens. The application is working with android devices good. But i installed application in amazon kindle fire tablet launched the application started good. I navigated from 1st screen to 2nd screen and pressed home button.
And again i launched application from applications menu. application is showing 1st screen instead of 2nd screen. Common behavior of android is should show the second screen.
What i know is while press on home button the application will go for onPause() state and launch application form launcher icon it will go fore onResume and show the screen where it has previously.
But application every time calling onCreate when launching from launcher icon. This happened in Amazon kinlde fire tablet only.
I am starting the 2nd activity from 1st activity using startActivity(intent); method
May i need to use any flag for kindle fire tablet.
Please suggest me as ASAP.
Thanks in advance.
Android doesn't guarantee that once you press back from an activity, it will remain in the onPause state. It depends on how aggressive the memory killer is, or how low the device memory is. What this means is, don't count on your application being in onPause state ever.
What you could do is, save the activity state in a shared preference, and let the Application object launch the corresponding activity, restoring UI state if required.

StartActivity intent fails when certain apps are running

I am running into a critical conflict of sorts. My app is a remote service which essentially starts an activity when the screen goes to sleep. How it does this is very simple via screen off broadcast receiver and then an explicit intent to start the activity as a new task. The activity is basically in charge of responding to key events and displaying some simple text.
Thanks to a few window flags added in 2.0, activities can do this. They can be created in a way that either puts them on top of the lockscreen, or completely dismiss the lockscreen. This way they basically have focus without the lockscreen having to be dismissed by user. The alarm clock in 2.0 uses the flags to wake up the device and show the alarm dialog. I use them to place my activity when the screen sleeps so the user sees a custom wakeup lockscreen. The reason we create it at screen off is to get rid of lag the user experiences at wakeup involving first seeing the lockscreen, then seeing the activity appear. Also doing it immediately at sleep allows it to have focus so it can handle key events effectively.
The process works perfectly except in certain apps. So far, it seems the bug is consistent while browser (and even dolphin browser) as well as the facebook app are running. The bug never happens in GTalk or Launcher. It is rare but can still be duplicated in the messaging app every so often. I can't figure out why my activity doesn't get created at sleep while these apps are active. My remote service still gets the screen off broadcast and does the startActivity for the explicit intent, and that's all I get in the log. My onCreate is not being called. Instead it gets called when we wake the screen up again.
I have tried, as a control, to hold the partial wakelock starting when my remote service gets created, and the issue persists. So I don't believe it is a problem that the CPU has gone to sleep. Since only these particular apps cause the issue to duplicate, I can't imagine why the activity start fails. What could those apps be doing to interfere with another app's ability to get created? I use singleInstance as the launch mode so that I can ensure that the activity will never be able to be recalled by user process. I want it to go away when user unlocks and it is working fine like this, as long as it is able to be created. The singleInstance ensures I can have the same lockscreen handle an intent to do something specific based on user actions that the remote service monitors.
my source code can be viewed on my project page. http://code.google.com/p/mylockforandroid/source/browse/#svn/trunk/myLock/src/i4nc4mp/myLock
the issue happens to both my CustomLockService and NoLockService variations. These two services will start Lockscreen or ShowWhenLockedActivity and the bug is witnessed. The build illustrating the bug's end result-- user has to try to unlock 3 times due to the bug because on wakeup when the oncreate finally succeeds, user is seeing the activity when normally it would have auto-dismissed thanks to key event logic that also isn't seeming to happen due to the delayed onCreate, so they have to send it to sleep again. Now that the activity is properly done being started, and screen is asleep, the expected functionality happens at next wakeup-- can be downloaded also from the downloads tab.
This seems like an extremely irrational thing to be caused only by specific apps. I am quite baffled and out of ideas for a solution unless I've made some critical mistake in my activity definitions.
The answer is actually a bug in android that has been in review for a while. It has to do with the home key. For some reason start activity calls as new tasks are getting stopped after the home key has recently been launched. I never noticed the connection during the testing of this. The bug was not consistent and the factor of consistency was whether home button had been used during the wake in question
Here is the bug report: http://code.google.com/p/android/issues/detail?id=4536

Categories

Resources