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.
Related
I have made an android app, when I close the app (using the home button) and reopen the app, it does not call onCreate() again. Instead it just loads from memory.
How can I make sure every time the app is opened onCreate() is executed?
EDIT:
The app has to do the same thing in onCreate() as in onResume().
When I copy the exact code to the function onResume() it does not work the same.
Therefore I want to close the app (and the process) completely (or do something similar) so onCreate() always gets called after reopening the app.
I understand that you want onCreate() to be called everytime app opens up butit does not. This is because your app will still be in recents when you are opening it again.
When you open app when it already exists in recents, your onStart() gets called. Try adding your logic in onStart().
If you want only onCreate() to be called everytime, then you must remove from recents when home is pressed. For that you need to make changes in Manifest fie for the activity.Try using the below for the activity inManifets :
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch="true"
when I close the app (using the home button)
That does not "close the app", for any typical definition of "close". The closest analogy, in a desktop OS, would be minimizing the app.
How can I make sure every time the app is opened onCreate() is executed?
You don't. Instead, you use other lifecycle methods that are more appropriate, such as onStart(), which will be called both after onCreate() (when the activity is first created) and when the activity is returning to the foreground from having been in the background.
I have put System.exit(1) in the onStop() function. After opening the app from recent apps, it runs onCreate().
So this worked for me.
Just do call onResume this will do the trick.
#Override
protected void onResume() {
super.onResume();
// TODO: do what ever you want
}
Have a look at this site: Activity-lifecycle concepts
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.
I am getting a problem since last two days.Actually when i run my app in any other device except the nexus tab,after pressing the home button the onDestroy() method not called.Its fine for me but when the same app is run in nexus tab the onDestroy() method is called after pressing the home button.What i want, i don't want to call onDestroy() method in nexus 7 after pressing the home button.Please sort out my problem.
Don't rely on onDestroy() being called. You should do everything you need in onPause() instead.
From the onDestroy() docs:
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here.
You don't get to decide that. As soon as your app goes into onPause Android can reclaim the resources used by your app by killing your app and going through onDestroy. You need to override the lifecycle events and handle saving the data necessary within them to prevent this.
When i unlock my device to see my application, the process in onResume() is launched. How do I cancel the events of unlock, to avoid onResume() from processing?
OnResume is always called when your Activity was in background (e.g. other App, Lockscreen, Homescreen... is shown).
Look at the Activity Life Cycle to check if you can move your code from onResume() maybe to onStart() to fix your issue.
To prevent unlock your device use below flag in your activity.
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
Code
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Note :
As stated on the Activity lifecycle docs, onCreate and onResume will always both be called the first time an Activity is started. When going back to Activity, at least onResume will be called
I am logging each onCreate() and onDestroy() calls. I found out that once I click power button on my Android (and on the emulator too!) the phone calls in my activity
> onDestroy();
> onCreate();
Which kills my game and then immediately reopen it from the beginning. Of course once user unlock the screen the game is presented in main menu with all progress killed.
Is it possible to override or disable this behavior?
When you press the power button, the screen lock usually kicks in. This may trigger a configuration change on the activity currently in the foreground (the screen lock is usually in portrait mode), causing it to be destroyed and recreated.
Declaring android:configChanges="keyboardHidden|orientation" for such activities in AndroidManifest.xml prevents them to be destroyed and recreated but also implies that you will handle the configuration changes by yourself (if necessary) by overriding onConfigurationChanged
You can't override when the onCreate() and onDestroy() methods get called (at least not without experiencing extraordinary amounts of pain). The best thing for you to do is to figure out how to work within the confines of when they get called. Save your state in onDestroy(). Make it so that your app can tolerate this call sequence because quite frankly, it supposed/has to. That's just how android works.