3

I'm looking for a single answer (but I might be asking the wrong question)

Question- does any event only get called once TOTAL until an activity is destroyed?

I ask because when my user rotates the phone to landscape oncreate and onstart are both invoked causing a reload of sorts.

I'm looking for an event that I could put behavior into that would only get run 1x (until the activity is killed)

Thank you in advance

Toran Billups
  • 27,111
  • 40
  • 155
  • 268
  • your subject and content are different....you want the method called only once for a activity or for an application...these are two different things... – Navin Ilavarasan Jan 27 '12 at 00:38

4 Answers4

11

If it is specific to the Activity just check your savedInstanceState parameter in the onCreate event. If it is null, run your code, if not, your code has already been run.

Example:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if(savedInstanceState == null) {
        // Run your code
    }        
}

savedInstanceState will always be null when onCreate is run for the first time, and it will be populated thereafter.

frikkenator
  • 241
  • 1
  • 3
1

You don't really specify what you're trying to do with it, so I can't guarantee this is appropriate for your use, but Application.onCreate is only called once.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Why does it call this when my view is reloaded during the landscape switch? I have an event that calls an http endpoint in the oncreate and it's being hit each time I switch orientation – Toran Billups Jan 27 '12 at 00:35
  • 2
    android kills your activity and re-creates them when there is a orientation change... – Navin Ilavarasan Jan 27 '12 at 00:43
1

If you want to eliminate the recreation of your activity on an orientationchange you can listen for configchanges in the manifest.

    <activity
            android:name=".MyActivity"
            android:configChanges="orientation" >
    </activity>

And then you can override onConfigurationChanged like so:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged( newConfig );

    LinearLayout main = (LinearLayout) findViewById( R.id.mainLayout );
    main.requestLayout();
}

to recreate the layout so that it matches the new orientation, without recreating the entire activity.

xorgate
  • 2,244
  • 1
  • 24
  • 36
  • This approach is not recommended. For example, it's wrong -- you need more values in `android:configChanges` than what you have, just for rotation events. Second, it does not handle all configuration changes. There are a handful of places where `android:configChanges` is a reasonable answer, but not that many, and fewer still given the advent of things like dynamic fragments and `setRetainInstance(true)`. – CommonsWare Jan 27 '12 at 01:01
0

Check http://developer.android.com/guide/topics/resources/runtime-changes.html to handle configuration changes and to maintain your huge data between them...if all you need to maintain between the configuration change is just the settings,you can use the onSavedInstanceState() and onRestoreInstanceState() callbacks and the given bundles.

Navin Ilavarasan
  • 1,271
  • 11
  • 15