Activity changes when lock screen - android

[SOLVED]
i got a question... When i lock my phone while my app is running, it changes automatically to the main activity (the one which is started at the beginning) when in unlock it.
Anybody knows how to handle that the activity at the moment of locking stays?
Thanks!

Does your activity call finish() in its onPause() method?If so remove it

Related

Is it possible to stop your MainActivity from being terminated?

I am running into problems where if a user navigates through several child activities without returning to the MainActivity in between, when they press the back button it closes to the Android Home screen. I never call finish() on my MainActivity, it doesn't happen consistently, and not from the same screens. It happens when I have gone through many different Activities without returning to the MainActivity.
I'm assuming that the system is terminating the Activity while its in the background, but I'm not sure. Is there anyway to ensure that my parent activity doesn't get terminated?
I did not create the project. I'm aware that the navigation structure he used isn't right, but as of right now I'm not in the position to fix it.
Please do not tell me to do things a different way. I am asking only if there is a way to keep a particular activity from being terminated by the system.
While it's not possible to stop your activity from being destroyed, you can check if there's no activities in the backstack, and restart your MainActivity when the back button is pressed in this particular case.
You could try to put a handler in it that doesn't do anything besides giving itself another task (which is to give itself another task) every few seconds.

close application when minimized

On start my app displays a splash screen and checks via network if the current user is still premium.
My problem: I started my app right before I went to bed and minimized it by pressing the home button. In the morning I launched the app again and it resumed the activity from the night. The app never really quit, my splash screen was not shown and and it couldn't check if the user is still premium.
So how can I achieve my app to be closed after a certain time (e.g. when the app is minimized)?
You should write the Premium user check logic in your onResume() method so that
if the activity is in pause or background state it will check the
logic every time it will be launched .
Don't try to finish app when it's minimized. Use Activity lifecycle callbacks.
#Override
protected void onResume (){
//check for changes here
}
If you want to end an activity, you call finish(). So you could record the time in onPause, then in onResume, check how long its been. If its been too long, call startActivity on your main activity, then call finish() to end the old one.
I think you should become more familiar with the Android Activity Lifecycle and think about which call back in your activity should you check if the user is premium

Android onPause: app closed || activity changed || screen off?

Is it Possible to see whats happening in onPause of an Android Activity?
I want to know if the app is
closing, going to background or screen is switched off
or
activity is changed or Sub-Activity is closed
is there a way to get this information?
You can see what it happening in the pause by looking at what is written in onPause().
I think you meant to ask, "What happens to the activity after onPause?"
To put it another way, you can't tell what is happening to the activity in onPause because the things that happen to the activity are taken care of in other methods. You'll have to look at these other methods at runtime.
Put a breakpoint in onRestart(), if this is hit, you can be sure that your activity was never destroyed.
Put a breakpoint in onDestroy(), if the activity is being destroyed, this breakpoint will be hit.

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.

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