Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
}
like image 254
shahooo Avatar asked Apr 09 '20 18:04

shahooo


People also ask

How do you prevent the fragment from loading twice on pressing the button?

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.

How do I know if my Android activity is recreated?

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. } }

How does activity come to the foreground?

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.

Can an activity start itself Android?

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.


2 Answers

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

like image 103
shahooo Avatar answered Sep 21 '22 21:09

shahooo


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.

like image 35
jeprubio Avatar answered Sep 20 '22 21:09

jeprubio