Threads start while screen still locked, want to prevent - android

I have a layout that has some control buttons and a custom view (MainView) in the bottom of the HorizontalLayout. The MainView starts a ControllerThread that in turn starts other threads. My goal is to not start the ControllerThread until the screen has been unlocked and the HorizontalLayout is definitely visible to the user.
(henceforth ControllerThread is _ctrlr)
I first naively put the _ctrlr.start() in the MainView's constructor. Then I placed it in onFinishInflation(), but this is called before the screen is unlocked. My last attempt was to place it in onWindowVisibilityChanged() and start the controller if visibility is set to VISIBLE, but even this is called prior to the screen being unlocked. I'm assuming that those functions are called prior to the screen being unlocked, since the _ctrlr is confirmed to be running.
Is there a method available in View's to check if the screen is locked/unlocked? Or do I have to maybe use the KeyguardManager?

Per blackbelt's suggestion, I moved the call to start the thread to the controller by creating a public class in the MainView that will start the controller when it's called. In OnResume of the MainActivity I call:
((MainView) findViewById(R.id.mainView)).startController();
The ControllerThread will now not start until the app is actually pulled up in the emulator after you unlock the screen.

Related

When android actually renders and displays the activity?

I am trying to understand on which stage android OS is rendering layouts, in order to be able to access views in my ListView. Thanks
The layouts are created in onCreate():
The entire lifetime of an activity happens between the first call to
onCreate(Bundle) through to a single final call to onDestroy().
The Activity is visible from onStart():
The visible lifetime of an activity happens between a call to
onStart() until a corresponding call to onStop(). During this time the
user can see the activity on-screen
Source: the documentation.

Why is onStop being called right after my Activity is launched?

I have an activity that needs to turn on the screen when it is started (just in case the screen was off). So in my onCreate, I have:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
|WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
|WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Using this straightforward combination, I am able to cause my activity to display whenever it is started from my background service (yes, this is a legitimate case).
The problem, however, is that there is a very strange lifecycle behavior when I start up my activity in such a case. Using extensive logging, I was able to figure out that the following 7-step process happens immediately after the activity is started:
onCreate
onStart
onResume
onPause
onStop
onStart
onResume
See that? For a simple activity start, onStart is called twice. And more importantly, onStop is mysteriously called, even though the activity was just started - and nothing happened that would cause it to stop.
I have tested this in many different scenarios and it appears that this strange behavior only happens when the screen is off and the activity is launched after it was destroyed. If the screen is on, or if the activity was stopped [but not yet destroyed] the activity launches normally, and onStart is only called once.
Bottom line: it appears that when my activity is launched and the screen is forced on, Android starts the activity, then stops it, and then starts it again for no apparent reason.
So: why is this happening? And is there anything I can do to workaround this (so that onStop isn't called until there is a legitimate cause for it to be)?
Notes:
The activity in question is using the singleTask launchmode
I have tried disabling the keyguard / lock but it has no effect
I am witnessing this behavior on a Samsung Galaxy Tab 10.1 running Android 3.2. I have not tested whether this applies to anything else...
I had a similar problem here:
Activity Lifecycle X Power Button X Lock Screen
The problem was that as my activity was forced landscape, when I turned on the screen it showed the lock screen in portrait and it was causing a configuration change and hence destroying the current activity.
The solution was to add a android:configChanges="orientation" on the Activity in my AndroidManifest.xml.
As suggested by #cyberhuman, the answer to this question OnPause and OnStop() called immediately after starting activity pointed me to the right direction.
My issue with the activity completing a full 'dummy' lifecycle before being actually displayed to the user was that my attempt to play the ringtone when the activity became visible was directly muted by the additional, subsequent "onStop()", and using flags to make the activity behave properly when launched when screen is off/when screen is locked/when screen is on/when activity is running in the background was not possible.
I finally solved my issue by overriding the onWindowFocusChanged(boolean hasFocus) method and launching the ringtone from there. Putting it here in case somebody has the same issue.
You could check for the situation in onStart, set a static or global variable, and then check the variable in onStop to override the standard behavior.

Android - Losing scope on UI elements after onDestroy()

I am trying to handle problems that occur in my application when the phone is plugged into certain types of chargers and put into "Car Mode" or "Driving Mode".
In the running application, onDestroy() is called and immediately followed by onCreate(), and the application starts again normally. However, subsequent calls to update UI elements (in the newly created main Activity) now have no effect, and it looks like I've lost scope on my layout.
RelativeLayout splash = (RelativeLayout) findViewById(R.id.splash);
splash.setVisibility(View.VISIBLE);
What could be ocurring onDestroy() that I'm not accounting for? I don't do much cleanup onDestroy because I didn't think I needed to.
The Activity has been detached from the UI by the time onDestroy() is called so having UI calls to it doesn't make any sense. If you need the splash to be shown, set it to View.VISIBLE in onCreate(), onResume(), or maybe onPause(). I'm not entirely sure if onPause() would act any different.
When the phone rotates the activity is destroyed and recreated. Plugging into a car charger usually forces the phone to landscape mode, thus rotating it (from portrait, most likely) and calling onDestroy. There is a way to prevent this behavior with some activity flags -- but Google advises against it.
We need to see some more code for this Activity to figure out what's going on.
Also, as DeeV points out, the activity is long gone by the time onDestroy gets called, so it might not be the right place to be doing whatever it is that you're doing -- but we need more code to be sure.
As a sidenote, sliding the keyboard up (on phone's that have slideout keyboards) will produce the same effect.

Unlock Android Image fails to Show

Whenever the Android device locks and I unlock it, the image that was displayed in the ImageView disappears. What do I need to do to redisplay it or prevent this from happening.
More:
I have a view that displays video, images or text depending on the context and three subclasses that extend the parent view. On creation, I replace the display view with the View object returned from createMediaPreview(), which each subclass implements.
Thanks in advance
You'll probably want to call createMediaPreview() again in the onResume() method:
http://developer.android.com/reference/android/app/Activity.html#onResume()
As roundhill mentioned, you need to setup your image in onResume(). OnCreate() only gets called when the Activity is first instantiated. When the phone goes to sleep, the Activity remains in memory, in sleep mode. When you unlock the phone, Android OS will bring it back to the foreground (it doesn't call onCreate() since it already finds this activity in memory, it already exists). Check the Activity's lifecycle here

how to Set already running activity, when user clicks on app icon on home screen

I have Two activities One splash screen, Player screen.
When user clicks on my app icon first splash screen is displayed and then player screen
When player activity is running, if user returns to the home screen
and then again clicks on app icon, the application is starting from the splash screen again.
can any one please help me out how to do any one of below
1) I need to close current running activity and reload application.
or
2) I need to resume to the player screen directly.
Please give me an example or reference to follow, Im beginner in android programing
Thanks In advance
You may want to overrive the onRestart() method - it will be invoked if your Activity has been stopped previously; while it won't if it's the first time it runs (or if it has effectively been killed in the meantime).
Be sure to read the Activity life cycle there: http://developer.android.com/guide/topics/fundamentals.html#lcycles to understand what happens when.
Check out the order in which the life cycle states appear. You could override onStart, onPause, onResume, onStop, onCreate, onDestroy etc functions by adding Toasts to see the sequence. Then you can override them as per your programming requirements.

Categories

Resources