Click here to Skip to main content
16,022,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, when I go through the side menu to another fragment and try to change the theme from there, there is a redirect to the reverse fragment, while the theme changes, how to make sure that when you click on "change theme" the application is not redirected to the main screen, I'm completely noob, please help

Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
        App.getApp(applicationContext).componentsHolder.getComponent(
            javaClass, MainModule(applicationContext)
        ).inject(this)

        super.onCreate(savedInstanceState)

        loadThemePreference()

        binding = getDataBindingView(R.layout.activity_main)

        navController = Navigation.findNavController(this, R.id.nav_host_fragment)

        headerBinding = DataBindingUtil.bind(binding.navigationView.getHeaderView(0))!!

        headerBinding.handler = this
        flashlightManager = FlashlightManager(this)
        brightnessRegulator = BrightnessRegulator(this)

        observeConnectionChanges()
    }


private fun updateTheme(mode: Int) {
        AppCompatDelegate.setDefaultNightMode(mode)
        val decorView = window.decorView
        decorView.invalidate()
    }

override fun recreate() {
        startActivity(getIntent());
        finish();
        overridePendingTransition(0, 0);

    }

private fun loadThemePreference() {
        val sharedPreferences = getSharedPreferences(Constants.APP_PREFERENCES, MODE_PRIVATE)
        val themeMode =
            sharedPreferences.getInt(Constants.THEME_MODE, AppCompatDelegate.MODE_NIGHT_NO)
        AppCompatDelegate.setDefaultNightMode(themeMode)
    }


<pre>override fun onNavigationItemSelected(item: MenuItem): Boolean {
        binding.drawerLayout.addDrawerListener {
            when (item.itemId) {
                R.id.notificationsViewPagerFragment -> navController.navigate(R.id.action_global_notificationsViewPagerFragment)
                R.id.tasksViewPagerFragment -> navController.navigate(R.id.action_global_tasksViewPagerFragment)
                R.id.rootEquipmentFragment -> navController.navigate(R.id.action_global_rootEquipmentFragment)
                R.id.defectsFragment -> navController.navigate(R.id.action_global_defectsFragment)
                R.id.drawer_item_get_data -> presenter.syncDataWithServer(LOAD_DATA_FROM_SERVER)
                R.id.drawer_item_send_data -> {
                    presenter.syncDataWithServer(UPLOAD_DATA_TO_SERVER)

                    this.lifecycleScope.launch(Dispatchers.IO) {
                        delay(300)
                        val cacheDir = this@MainActivity.cacheDir
                        cacheDir.deleteRecursively()
                        val codeCacheDir = this@MainActivity.codeCacheDir
                        codeCacheDir.deleteRecursively()
                        val cacheDir2 = this@MainActivity.externalCacheDir
                        cacheDir2?.deleteRecursively()
                    }
                }

                R.id.switch_light_dark -> {
                    val currentNightMode = AppCompatDelegate.getDefaultNightMode()
                    val newMode = when (currentNightMode) {
                        AppCompatDelegate.MODE_NIGHT_YES -> AppCompatDelegate.MODE_NIGHT_NO
                        AppCompatDelegate.MODE_NIGHT_NO -> AppCompatDelegate.MODE_NIGHT_YES
                        else -> AppCompatDelegate.MODE_NIGHT_NO
                    }
                    saveThemeMode(newMode)
                    updateTheme(newMode)
                }

                R.id.drawer_item_exit -> presenter.logout()
            }
            binding.drawerLayout.removeDrawerListener(it)
        }
        closeDrawer()
        return true
    }


What I have tried:

at first, the application completely shut down, and I redefined the recreate() method, after that the application did not crash so that it would not throw me back, I tried to save the navigation ID so that when onCreate() redirects navigation to the desired fragment, but nothing worked
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900