3

I create an app from the template, and populate with some Logs as below

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MainApplication.something++
        Log.d("Tracking", "onCreate ${MainApplication.something} ${Thread.currentThread()}")
        setContentView(R.layout.activity_main)
    }

    override fun onStart() {
        super.onStart()
        Log.d("Tracking", "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d("Tracking", "onResume")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d("Tracking", "onConfigurationChanged")
    }

    override fun onPause() {
        super.onPause()
        Log.d("Tracking", "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d("Tracking", "onStop")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d("Tracking", "onSaveInstanceState")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("Tracking", "onDestroy")
    }
}

and

class MainApplication: Application() {
    companion object {
        var something = 0
    }

    override fun onCreate() {
        super.onCreate()
        Log.d("Tracking", "Application onCreate $something ${Thread.currentThread()}")
    }
}

When I run it, I notice that sometimes it launch 3 times. (some other times, it launch 2 times and sometimes 1 time).

The log as below

2021-11-04 23:55:31.409 7810-7810/com.example.empty D/Tracking: Application onCreate 0 Thread[main,5,main]
2021-11-04 23:55:31.445 7810-7810/com.example.empty D/Tracking: onCreate 1 Thread[main,5,main]
2021-11-04 23:55:31.409 7810-7810/com.example.empty D/Tracking: Application onCreate 0 Thread[main,5,main]
2021-11-04 23:55:31.445 7810-7810/com.example.empty D/Tracking: onCreate 1 Thread[main,5,main]
2021-11-04 23:55:31.508 7810-7810/com.example.empty D/Tracking: onStart
2021-11-04 23:55:31.445 7810-7810/com.example.empty D/Tracking: onCreate 1 Thread[main,5,main]
2021-11-04 23:55:31.509 7810-7810/com.example.empty D/Tracking: onResume
2021-11-04 23:55:31.508 7810-7810/com.example.empty D/Tracking: onStart
2021-11-04 23:55:31.509 7810-7810/com.example.empty D/Tracking: onResume
2021-11-04 23:55:31.509 7810-7810/com.example.empty D/Tracking: onResume

Initially, I thought it's the Activity gets launched 3 times, but then when I log out the Application onCreate, it also 3 times. I explicitly put a static variable something, and clearly see that the Applications are 3 different instances (since the variable didn't get incremented across the 3 times).

To confirm it is not because configuration change or activity got killed (as per explained in https://stackoverflow.com/a/3588750/3286489), I also log out when onPause, onStop, onSavedInstanceState and onDestroy, none of them get called.

Does anyone know why did the application start 3 times when only launch once?

Note: I compiled on Android SDK 31 and 30, and Emulator API S and API 29, all of them have the same behavior. Also tried on the actual device Android 8.0. Same behavior shown.

Update I notice it only happens after the second compile. Perhaps it's just a repeated uncleared Log (though I have clear it).

Elye
  • 53,639
  • 54
  • 212
  • 474
  • do you get this behavior only on sdk 31 and 30? Also, did you try to run the app on a device? Is the behavior the same? – gts13 Nov 04 '21 at 21:02
  • Tried on Device Android 8.0 as well. That happens too. – Elye Nov 05 '21 at 00:51
  • Probably same issue as https://stackoverflow.com/questions/5771616/logcat-showing-information-3-times-on-avd – Elye Nov 05 '21 at 03:08

1 Answers1

3

Apparently, it's an Android Studio filtering issue. I'm having the Tracking there to filter the log. And it shows multiple log of the same type.

When I remove a character from the log, then the filter get corrected. After putting back the character, the filter is still okay.

See the GIF below.

enter image description here

Elye
  • 53,639
  • 54
  • 212
  • 474