Android calling onCreate() after finish() when in landscape - android

I have an activity that starts the Vibrator system service in its onCreate method, then when the user pushes a button it cancels the vibrator, then calls finish() to close the activity.
This activity is brought up via the AlarmManager, so when it gets closed it will return the user to whatever app they currently had open (not necessarily mine).
The problem I'm having is if my activity is in landscape mode, and the user is brought to a screen that doesn't support landscape (such as the home screen) when the activity closes, my application switches to portrait and calls onCreate() before actually closing my screen. So the steps causing this problem are as follows...
The activity is lauched in portrait mode
onCreate method gets called, which starts the vibrator
The user rotates the phone to landscape mode
onCreate is called again, but because onSaveInstanceState is not null, I can skip starting the vibrator again
The user pushes the button to close the screen
I call vibrator.cancel()
I call finish()
Because the screen the user will be brought back to a screen that doesn't support landscape mode, my activity calls onCreate()
savedInstanceState equals null, so the vibrator gets started again
My app is closed with the vibrator still running
Currently the only way I can think of to rectify this is to make my activity only support portrait mode, but I'd like to avoid that if I can. Does anyone know a way in which I can prevent onCreate() from being called after I call finish()?

why dont you call method
onPause() or onStop() and inside this methods call vibrator.cancel()
onStop() always gets called when your app is not visible anymore.
check out this flowchart

Related

The activity loses focus when home key button is pressed on Android

When I press the home button on my phone, and when I return to the application, it loses the focus completely, it loses track of where activity was; i.e. the Activity returns to principal state. How to avoid that so that when I press the home key and return to the app; to return to the where the activity was?
I need the solution to work for all devices?
Thanks.
Well when you press the home button, the activity's lifecycle methods will be called (first onPause(), then onStop()), and then when you return, if in the meantime your operating system didn't call onDestroy() (this happens when you force close an app or are running out of memory), your Activity will call onResume() method. If it was destroyed, it will call onCreate and start from the beginning.
So basically what you need to do is override (CTRL+O in Android studio to open the override menu) these methods to save your application state, save the important parameters of your app (since you've not shared any code, I cannot presume what these are) and to then later restore the state your app had previously come to.

Android Activity life cycle - onPause() and onResume()

In the Android developer diagram, I saw that onResume() is always called before onPause(). Assuming the user starts a new Activity, why should onPause() be preceded by onResume()?
I mean:
OnResume can be called in 2 occassions:
1) when user starting new activity (before OnPause)
2) when activity is in background and if the user brings the activity back to the
foreground
I expect in every case, something else should be done.
You are getting it wrong. Whenever an activity is created, onResume is called just after onStart. Whenever your activity goes back stack onPause is called. Again if your activity comes back to foreground then onResume is called. So, it is not like, onResume is called before onPause. Whenever activity is returning from onPause state, onResume gets called instead of onStart or onCreate. This happens so that Android does not have to create Activity instance again and again though those instances are not properly destroyed. This is quite memory efficient too.
NOTE: If you app is running and the user presses back button or home button, then the Activity goes through onPause() and onStop() state. After this if the user is again coming back to your app then, onRestart(), onStart() and onResume() will be called sequentially.
Then when the activity is only in onPause() state ? When a dialog surfaces on top of your activity or your activity is getting displayed in Split screen but it doesn't have focus (user is not interacting with your app). On these cases, activity goes to onPause() state only.
onResume() is always called before onPause()
This is correct. onResume is always called when the Activity is launched for the first time, before onCreate, and when the Activity is being resumed (user navigates back to your Activity)
Assuming the user starts a new Activity, why should onPause() be
preceded by onResume()
onPause is only called when the Activity is put to background, or before onDestroy if the Application is being destroyed. So onPause is always being called after a call to onResume has been made. Why? Because that's the lifecycle of the Activity as defined by the Android framework.
The life cycle of the activity is as follows
Fresh start via onCreate(), onStart(), onResume .... and close via onPause()->onStop()->onDestroy()
Yellow background: Activity goes into background and thus is no longer visible. The user returns back to the activity.
e.g.
Switch off the phone screen while the activity is running: onPause()->onStop()
Switch on the screen again: onStart() -> onResume()
Green background: The activity stays in the visible screen area but is not active
e.g. Activate multiple windows (split screen) occupying one part of the screen each and tip on your app to make it active
tip on the other app: onPause() is called in your app as it goes into pause but is still visible
tip on your app: onResume() is called
Here is an example of a split screen with two apps:
see android documentation on activity life cycle for details

Tracking the activity life cycle

I have requirement wherein I have to display a lock screen if the user moves out of the application.
Hence, the structure is: Activity A extends Activity B.
Wherein Activity B is the deciding activity : "was application in backgound".
If so it launches the lock activity.
Now, say I am on activity A and receive a phone call. Hence the app gets into the background.
When it resumes I can see the glimpse of Activity A for a fraction of second and then comes the lock activity.
Can there be any solution to avoid that glimpse of Activity A?
You can see the lifecycle of activity from official doc
You are using activity B just to track whether activity is alive. I am not sure if it is necesary.
If activity goes to background onPause() method is called, it means activity is not visible (it might be both screen lock or home button pressed), and onResume() is called when activity is visible again. In Activity A if you override onPause method and launch your lock activity, it should work. (Or set a boolean onPause and launch lock activity on resume(you might see Activiy A though)
Good luck

How to disable lock screen on Android

I want to disable the lock screen in a certain app. My problem is that this also disables the lock screen for the whole phone. So I added lock.reenableKeyguard(); to the onPause() method, but this is called not only when the user exits the app but everytime a new activity starts in that app. Do I have to disable the lock screen in every activity and add this onPause method? Is there way to manage this only in the first activity that is shows when the app starts?
Btw I also reenable the lock screen in the onStop and onDestroy methods. The other problem with onPause is that it is also called when I turn off the phone's screen (by pressing the power button or what is that called), so when I first open the app, I turn off the screen then turn it on, there is no lock screen, but when I turn it off-on again, there is it, as I reenable it in the onPause method.
After playing with this a couple of hours I figured out I forget to implement onResume():
public void onResume() {
lock.disableKeyguard();
super.onResume();
}
This way every time I open the activity/turn on the screen, the lock screen is disabled.

Why does my android app call onCreate() when sleep initiates in my view but not from home screen?

My app seems to behave perfectly except I do not understand one thing. When I press the home button and return to my app, the previous state is restored (automatically). When I press the home button and then the phone goes to sleep in the home screen (or any other view except my own) and I return to my app, the previous state is restored (automatically).
Now, the kicker is that when the phone goes to sleep with my activity on top, it does NOT automatically restore the previous state but seems to call onCreate() because the starting initialization occurs and my app behaves as if it just started.
I am able to save the instance and recall the stored state using saveInstanceState() so ultimately the problem is solvable, but my question is:
Why does Android call onCreate() if it went to sleep from my activity but not from another?
And if it does not call onCreate() what is happening and why do I have to bother with saveInstanceState() since in every instance (barring low memory) my app returns the user to their last "placemark" EXCEPT when the phone goes to sleep from my activity.
Check whether your activity has locked the orientation to portrait or landscape, if you have locked the orientation, it will call onCreate() as it changes the orientation before it sleeps and when it wakeup.
you can solve this by adding android:configChanges="orientation" in to activity tag in the manifest.
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="orientation"
/>
When I am having issues with the life cycle methods what I would do is add onCreate,onPause,onResume etc etc in all my activities with a log.D("in onPause()") inside to trace down exactly which method is being called at what time.
Android would only call onCreate if it previously destroyed your activity, in which case onDestroy would have been called. The situations where your activity state was preserved without onCreate being called were simply ones where your activity was never destroyed, it was left running the whole time.
Here is an overview of when, and how, to save/restore your instance state.

Categories

Resources