0

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

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Addev
  • 31,819
  • 51
  • 183
  • 302
  • You could use isFinishing() in onDestroy() to check if the activity is finishing. – Ron Aug 12 '11 at 13:24

5 Answers5

2

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 in onConfigurationChanged().

One option is to move the initialization to a new function that gets called from both onCreate() and onConfigurationChanged().

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 in onDestroy() I would install an Alarm for, say, 10 seconds (which I imagine is plenty of time to have onCreate() called again). In onCreate() I would cancel the alarm. When the alarm fires, I would perform my save actions.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
mah
  • 39,056
  • 9
  • 76
  • 93
  • Thanks for all your answers. The alarm seems to be the best option for leaving Android to manage the rest of the parts. How should I implement it? Maybe with an Handler and a delayedpost/removecallbacks? – Addev Aug 12 '11 at 11:28
  • When you install an http://www.androidcompetencycenter.com/2009/02/android-basics-alarm-service/ you specify an Intent (PendingIntent, actually) for a broadcast to be received when the alarm expires, so the decision is pretty much made for you already. You'll likely need to arrange for data you're saving to outlast your Activity just in case the Activity has been destroyed before the alarm expires -- you can do this by attaching it to your Application instead (see http://trace.adityalesmana.com/2010/08/declare-global-variable-in-android-via-android-app-application/). – mah Aug 12 '11 at 11:43
1

Declare buttons and labels as class variable.

jainal
  • 2,973
  • 2
  • 20
  • 18
  • This would nor work since he calls setContentView() again; the class variables would be stale since they would point to layout items that don't exist any more. – mah Aug 12 '11 at 11:11
1

setContentView recreates your view, so you must rebind your data. the best approach would be a function called both from onCreate() and onConfigurationChanged(), with layout creation and bindings.

Raiv
  • 5,731
  • 1
  • 33
  • 51
1

If you don't want to anything to happen when orientation changes occur, than you should not re-setContentView(). Basically you are telling your app: "DO NOTHING WHEN ORIENTATION CHANGES". So, remove the setContentView inside the onConfigurationChanged() or test for which orientation currently is active and then load desired layout resources.

When orientation changes onDestroy() is called because the changes restart your entire activity.

Read more here: http://developer.android.com/guide/practices/screens_support.html#qualifiers http://developer.android.com/guide/topics/resources/providing-resources.html

Orientation testing: Setting the background of an Activity

Community
  • 1
  • 1
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
1

Could you do that stuff in overriden finish() of the activity?

Ron
  • 24,175
  • 8
  • 56
  • 97
  • finish() is not a callback, its a method you can proactively call to inform the Activity it's done -- but it's not the only way an activity will go away. – mah Aug 12 '11 at 11:45