OnConfigurationChanged followed by OnStop ()? - android

I am stuck in a strange situation....
I have declared android:configChanges="orientation|keyboardHidden" in the manifest file of the Activity. So, ideally I want my Activity onConfigurationChanged() to be called whenever, I rotate the device. But, that does not happen.
Inspite of having these attributes in the manifest file, the Activity onStop() is called first followed by onCreate, (the onConfigurationChanged() is never called) when I rotate the device ----- This is the issue I am facing.
My expectation is --- Whenever, I rotate the device, onConfigurationChanged () to be called first, then onStop() and then onCreate().
What is the mistake that I am doing?
Is this at all possible ?

No, not possible.
You put android:configChanges="keyboardHidden|orientation|screenSize"in your manifest to handle configuration changes yourself.
You override the onConfigurationChanged() method and take care of configuration changes there.
onStop() and onCreate() are never called again, only onConfigurationChanged() is called during a configuration change in this case because the activity is never killed.

Related

What is the difference between app goes to background and screen locks

I am building a camera app everything works file when i put it to the background but when i explicitly press the lock button it does not work,i am not finding and difference between the callback methods being called or am i missing something please help.
Put log line inside these methods
onPause
onSaveInstanceState
onStop
onDestroy
onResume
onRestoreInstanceState
onStart
onCreate
And make sure the same methods are called in both situation.

Preventing from onDestroy being called screen off button pressed

I have Activity that gets populated with info. When I press the screen off button and come back to the Activity, the info is not there anymore.
Btw, If I use the home button the info doesn't disappear. On logs, it shows it doesn't call onDestroy;
Therefore, onDestroy discards the info. I don't want onDestroy. HOw could I make that happen.
Thanks.
OnDestroy will always be called. You should save data and than recreate Activity on onCreate. Everything is nicely explained here. Read section "Saving Persistent State". Also read about lifecycle of Activity.
Try to save activity's info on onSaveInstanceState() and restore the state on onRestoreInstanceState().
This approach also helps you also in configuration changes state.
onSaveInstanceState() is called before destroying activity and onRestoreInstanceState() is called just before recreating the activity.
Follow this for more details.
Probably duplicate of Android activity onDestroy() called on screen lock.
This behaviour usually occurs at landscape orientation activities. Add
android:configChanges="orientation|keyboardHidden|screenSize"
to your AndroidManifest.xml activity tag.

onDestroy is calling When phone screen Off

i created a sip call. When phone screen is turned on and if i receive any incoming call, everthing is OK. I mean that onStart() is called and onDestroy() is not called. So i am able answer the call.
But When phone screen is off and an incoming call is received. i see that onStart() is called and onDestroy() is also being called. I see that android process automatically kills this activity.
This problem is not in android 2.3 version. i saw this in the latest version.
Any Solutions? my onDestroy() method should not be called. it should be called only when i come out of the activity.
Yes, the problem occurs when using screen "Landscape" only, because the screen changes to "portrait" internally.
To prevent this, call onDestroy() in case screen goes ON or OFF.
add to AndroidManifest.xml File with "orientation|screenSize" attribute.
add onConfigurationChanged() method to your activity
AndroidManifest.xml
<activity
…
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
And add to your activity:
…
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
onDestroy method will call i your app is get force close or.. app is closed.. may be your app get closed by instance.. on onStop, onDestroy will call wen you press back button,. onDestroy wont call wen you press home button
You can go check in your developer options(On your latest version device), if you have enabled the 'Don't keep Activities' option. You can disable it. This could be one reason why onDestroy() is being called only on your latest version device.
Did you set your screen orientation as ScreenOrientation.LANDSCAPE_FIXED or ScreenOrientation.LANDSCAPE_SENSOR in onCreateEngineOptions()?
I found that onDestroy would be called when using ScreenOrientation.LANDSCAPE_FIXED. But if I use ScreenOrientation.PORTRAIT_FIXED, onDestroy is not called when turning screen off.
I'm still not clear about the reasons behind this problem, but I think this is a clue to solve it.

android: application crashes if orientation changes in a subactivity

My main activity handles changes into portrait mode just fine. However, if I launch a very simple activity via startActivityForResult() and switch to portrait mode before finishing it, my main activity crashes because some of its variables are set to null.
It looks like my connection to the Service is not being recreated. I tried manually calling doUnbindService(); doBindService(); inside of onActivityResult() but onServiceConnected() is still not being called.
What's the proper way to handle the Activity lifecycle here?
EDIT: I see in the log file that onServiceConnected() is indeed being called -- but it's being called after onActivityResult().
For you second Activity add this
if your android:targetSdkVersion="12" or less
android:configChanges="orientation|keyboardHidden">
if your android:targetSdkVersion="13" or more
android:configChanges="orientation|keyboardHidden|screenSize">
in manifest.xml
The problem was that I was referring to objects which don't exist until onCreate() is called again. I needed to queue my answers locally and wait for those objects to be recreated to avoid crashes.

Why onRestoreInstanceState() never gets called

I am trying to save data in my activity and than restore it.
I save data in on onSaveInstanceState() and then I try to restore the data in onRestoreInstanceState().
I setup breakpoint, the method onSaveInstanceState() get called. But onRestoreInstanceState() or onCreate() never did.
Here is my steps:
start my Activity.
press 'Home' button on the phone. onSaveInstanceState() get called.
Click the icon in the launcher and launch my Activity again.
At this time, only onRestart() get called. But not onRestoreInstanceState() or onCreate().
Does anyone know why?
Well, if onRestart() is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.
onCreate() method is only called when your Activity's onStop() is called and the process is killed.
Please refer the Activity life cycle Android Activity Life Cycle for a clear understanding.
You may want to check whether the onStop() method is called and if your process is killed. I do no think that your process gets killed by the scenario which you have described.
the onRestoreInstanceState() method is very tricky. I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.
From doc:
The system calls onRestoreInstanceState() only if there is a saved state to restore.
I have asked similiar question earlier on here
Here's some steps to test out onRestoreInstanceState():
Press home screen
Kill the app through adb
Launch your app again
Follow these steps (Using Android Studio):
Create New Logcat Filter, e.g. AppState
Launch the app on your emulator. You will see:
I/AppState﹕ onCreate
I/AppState﹕ onStart
I/AppState﹕ onResume
Press Ctl-F12 to rotate the emulator. You will see:
I/StateChange﹕ onPause
I/StateChange﹕ onSaveInstanceState
I/StateChange﹕ onStop
I/StateChange﹕ onDestroy
I/StateChange﹕ onCreate
I/StateChange﹕ onStart
I/StateChange﹕ onRestoreInstanceState
I/StateChange﹕ onResume
This causes the destruction and recreation of the activity by making a configuration change to the device, such as rotating from portrait to landscape.
See the link below for how to test onSaveInstanceState() and onRestoreInstanceState() on a real device or in the emulator.
This method uses the AlwaysFinish setting, which is simpler and faster than killing processes. This method also provides Activity-level control rather than process level control:
http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/
This is my solution for real device so onRestoreInstanceState get it called.
in manifest on related activity remove this part android:configChanges="orientation"
in manifest on related activity remove this partandroid:screenOrientation="portrait"
in your activity remove this line if it's there setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
on your device enable rotating from setting - display - auto rotate.
then run your app, rotate your device.
that's it.

Categories

Resources