Android: onCreate() getting called multiple times (and not by me)
Asked Answered
H

5

18

There is something I don't quite understand right now.

My main activity class creates a Service, which creates a new thread that waits for a TCP connection. Once one comes in, it will start a new activity:

Intent dialogIntent = new Intent(getBaseContext(), VoIPCall.class);
dialogIntent.putExtra("inetAddress", clientSocket.getInetAddress());
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);

After that, the onCreate() method of that class gets run. It will create 2 threads: one records and send data, the other one receive and plays data. Those threads have a forever while loop.

For some reason, I notice that the onCreate() of that last class gets called again, which makes my program crash. I do not understand why it is called again as only the 2 threads are running, there is no user interaction. The documentation says: "Called when the activity is first created.". The activity is already running and I am not trying to create it.

Could someone please explain me this behavior?

Hornsby answered 29/11, 2010 at 0:4 Comment(0)
K
14

Android will recreate your activity after certain "device configuration changes". One such example is orientation. You can read more here... http://developer.android.com/guide/topics/resources/runtime-changes.html

Perhaps something in your threads is doing something which is considered a configuration change?

If that's the case you might find it useful to extend the Application class instead and do your initialization there. See this post... Activity restart on rotation Android

HTH

Kaliski answered 29/11, 2010 at 1:49 Comment(1)
I have banged my head against the wall for this. I knew about this behavior but never considered grrrrrrrr. extempore great!!!Heath
G
8

I was experiencing an Activity called twice on some Samsung devices. I solved it adding android:launchMode = "singleInstance" on the Activity tag on Manifest. I hope this can help.

Gypsy answered 14/7, 2015 at 13:9 Comment(0)
C
2

This happened to me once when I used "Don't save actions" in the app section of the developer options. Be sure you have turned this off.

Continuo answered 6/6, 2013 at 15:47 Comment(1)
You should get a gold medal for this observation.Roer
P
1

Similar to this question where orientation change is causing the device to reconfigure:

Activity restart on rotation Android

My preferred answer:

https://mcmap.net/q/21904/-activity-restart-on-rotation-android

Instead of trying to stop the onCreate() from being fired altogether, maybe try checking the Bundle savedInstanceState being passed into the event to see if it is null or not.

For instance, if I have some logic that should be run when the Activity is truly created, not on every orientation change, I only run that logic in the onCreate() only if the savedInstanceState is null.

Otherwise, I still want the layout to redraw properly for the orientation.

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_game_list);

    if(savedInstanceState == null){
        setupCloudMessaging();
    }
}
Passionless answered 16/11, 2019 at 4:10 Comment(0)
R
0

I have observed this issue when you are trying start an activity with values in the intent.

Below is an example where Activity_A calls Activity_B and passes values in the intent to be collected in Activity_B:

Intent intent = new Intent(this, activityB.class);
intent.putExtra("val1", someValue1);
intent.putExtra("val2", someValue2);
intent.putExtra("val3", someValue3);
this.StartActivity(intent);

In this case, you can set the android:launchMode="singleInstance" or android:launchModel="singleTop" in your AndroidManifest.xml and Activity_B will only launch once. Hope this helps.

Rhizocarpous answered 22/5, 2017 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.