Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 261
James Avatar asked Nov 29 '10 00:11

James


People also ask

Why is onCreate called twice?

onCreate will get called when your activity has been destroyed and recreated, which happens any time the device is rotated, the keyboard is opened, or you switch apps and the system decides it's time to reclaim some memory and kill off your app.

Is onCreate called only once?

The `onCreate()` lifecycle method is called once, just after the activity is initialized (when the new Activity object is created in memory). After `onCreate()` executes, the activity is considered created.

How many times does onCreate run?

OnCreate is only called once. Am I misunderstanding something? Attachments: Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Why do we need to call setContentView () in onCreate () of activity class?

onCreate() method calls the setContentView() method to set the view corresponding to the activity. By default in any android application, setContentView point to activity_main. xml file, which is the layout file corresponding to MainActivity.


2 Answers

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

like image 54
xtempore Avatar answered Nov 10 '22 05:11

xtempore


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.

like image 38
lm2a Avatar answered Nov 10 '22 06:11

lm2a