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()
}
How do you prevent the fragment from loading twice on pressing the button? You can disable the button after opening the activityand when activity finish,,re-enable itu can detect the finish of second activity by calling onActivityResult function.
You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy . @Override protected void onDestroy() { super. onDestroy(); if (isFinishing()) { // wrap stuff up } else { //It's an orientation change. } }
Activity or dialog appears in foregroundWhen the covered activity returns to the foreground and regains focus, it calls onResume() . If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state.
Yes it is. If your requirement are like that then there is no harm in doing that. If you use this then dont forget to call finish(). finish() will remove the activity from backstack so when you press back you dont return to previous instance of same activity.
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
Activities are restarted in some scenarios like an orientation change, there is nothing wrong with this.
Instead of preventing the activity from restarting, which is part of its lifecycle, another thing that you could do and is what I encourage you to do is to use a ViewModel to handle this tasks as it is recomended in the recommended app architecture so that when your activity gets restarted and asks for its ViewModel:
viewModel = ViewModelProviders.of(this).get(MainActivityViewModel::class.java)
it reuses the same ViewModel and the tasks continue as if nothing has happened.
Actually, if you want to follow that architecture the tasks should be done in a repository, but the ViewModel should be in charge of it and it does not be recreated when the activity is restarted.
I recommend you to do the Android Kotlin Fundamentals 05.1 codelab about this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With