Reputation: 31
Does onCreate() get called twice during first run (during installation)?
I've seen a couple of similar questions regarding this, but they are sufficiently different, and do not answer my question.
In Android apps made using Java in Android Studio, when the app installs for the first time, why does it call the onCreate() twice? I experienced some issues due to this, while building an app, so I made a fresh project with an empty activity, with just Log.d statements to test it, and it seems to be true! I want to know why this is the case? Depending on use case, this might not be a problem but it was a tremendous problem for me.
MainActivity.java:
package com.example.oncreatetestapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("TESTLOG", "onCreate() triggered.");
}
@Override
protected void onPause() {
super.onPause();
Log.d("TESTLOG", "onPause() triggered.");
}
@Override
protected void onResume() {
super.onResume();
Log.d("TESTLOG", "onResume() triggered.");
}
}
Logcat during installation run:
---------------------------- PROCESS STARTED (11793) for package com.example.oncreatetestapp ----------------------------
2023-06-02 12:06:15.698 11793-11793 TESTLOG com.example.oncreatetestapp D onCreate() triggered.
2023-06-02 12:06:15.711 11793-11793 TESTLOG com.example.oncreatetestapp D onResume() triggered.
2023-06-02 12:06:18.326 11793-11793 TESTLOG com.example.oncreatetestapp D onPause() triggered.
2023-06-02 12:06:18.370 11793-11793 TESTLOG com.example.oncreatetestapp D onCreate() triggered.
2023-06-02 12:06:18.372 11793-11793 TESTLOG com.example.oncreatetestapp D onResume() triggered.
Logcat during subsequent runs:
---------------------------- PROCESS STARTED (12748) for package com.example.oncreatetestapp ----------------------------
2023-06-02 12:07:55.563 12748-12748 TESTLOG com.example.oncreatetestapp D onCreate() triggered.
2023-06-02 12:07:55.575 12748-12748 TESTLOG com.example.oncreatetestapp D onResume() triggered.
I changed nothing in the new Android project apart from the MainActivity, to which I added Log lines for debug purposes. Also, this seems to be a very basic issue, yet I couldn't find a single discussion regarding this on the internet. Request Android developer community to pitch in and help!
Upvotes: 3
Views: 732