2

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!

Shuvam
  • 21
  • 2
  • Where do you test your app? May be another installed app causes this behavior. – Robert Jun 02 '23 at 07:18
  • Please try something. After installing the app on the device, go to settings and force stop it. Now open the app from the HOME screen. Do you see the same behaviour? – David Wasser Jul 28 '23 at 15:47
  • What happens if you don't open the app from the installer, but just install the app (without starting it from the installer) and then open the app by clicking the app icon on the HOME screen. – David Wasser Jul 28 '23 at 15:48
  • Was this ever solved? I'm getting this exact issue on a Samsung A12, to my knowledge the issue appeared after updating the OS on the device a few days ago, I can't seem to reproduce this issue on any other device/emulator – Dan Gavrielov Aug 27 '23 at 08:34
  • After the recent mandatory playstore update (targeting android 13) my app showed the same behaviour. However, after uploading it to the playstore, installing it from the playstore, and then starting it from my UI... the problem did not persist. Just like @DavidWasser proposed. – Danny E.K. van der Kolk Aug 28 '23 at 11:53
  • 1
    You were most likely seeing this longstanding Android bug: https://stackoverflow.com/a/16447508/769265 – David Wasser Aug 28 '23 at 14:08

0 Answers0