I have a little problem to detect when the application is finished. I need to do some actions onDestroy like save the parameters into the database and make a final connection to the server.
The problem is that if I put the code in onDestroy its is called when the orientation changes for example. Putting
android:configChanges="orientation|keyboardHidden"
in the manifest for that activity the landscape/portrait layouts don’t swap. And adding
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
Changes the layouts but the buttons and labels do not get the onClickListeners and the text labels correctly. How can I solve that? Thanks
The problem is that your layout items aren’t initialized again because you’re initializing them in your
onCreate()function, and then you’re disrupting them with a new layout inonConfigurationChanged().One option is to move the initialization to a new function that gets called from both
onCreate()andonConfigurationChanged().Another option is to use the
android:onclick=""(and related) attributes in your layout.The option I would choose is different though. I would allow Android to manage orientation (and to call
onDestroy()) and inonDestroy()I would install an Alarm for, say, 10 seconds (which I imagine is plenty of time to haveonCreate()called again). InonCreate()I would cancel the alarm. When the alarm fires, I would perform my save actions.