androidandroid-activityandroid-lifecycleandroid-darkmode

Android Activity is getting instantiate twice while using Dark Mode


My launcher activity i.e. MainActivity is getting instantiated twice while using AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) which is leading to two network calls and making weird behavior.

Is there any to control this and make to initialize only once?. I've tried using launchMode = "singleTop" and "singleInstance"

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
    mRequestQueue = Volley.newRequestQueue(this)
    Log.e(TAG,"Skillet")
    loadStateData()
    initializeListeners()
}

Solution

  • Found a solution after trying a few of my practices

    override fun onCreate(savedInstanceState: Bundle?) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        }
    

    Call dark mode function before super of onCreate()

    It will prevent instantiating activity twice