Home > Back-end >  Kotlin, How do I dismiss an App by pressing the Back button Activity which is not the launcher activ
Kotlin, How do I dismiss an App by pressing the Back button Activity which is not the launcher activ

Time:01-05

I have a login activity with navigation fragments , one is the splash screen and then navigate to Login Fragment this is the Launcher Activity and it checks if the user is already logged in and then start de Initial Activity if is already logged in. In the Initial Activity i have a navigation fragments but when the user press the back button the Activity Launch start instead of close the activity. I want the user to close the app like the same behavior that occurs when back button is pressed on the launcher activity. How do i achieve this?

This is the splash fragment in the Launcher Activity:

fun initListeners() {
        val DURATION = 2500
        user_app.toString()

        val handler = Handler()
        handler.postDelayed({
            if (user_app.isEmpty() && pwd_app.isEmpty()) {
                navigationToLogin()
            } else {
                Toast.makeText(safeActivity, "Sesión iniciada ${OPERATOR_APP.getPreferenceToString().toString()}", Toast.LENGTH_SHORT).show()
                startActivity(Intent(safeActivity,XTInitActivity::class.java))
            }
        }, DURATION.toLong())
    }

    fun navigationToLogin() {
        val navigate = SplashFragmentDirections.actionSplashFragmentToXTLoginFragment()
        findNavController().navigate(navigate)
    }
}

I try when the methon onBackPressed whit its callbacks but its now deprecated for most recent API

CodePudding user response:

override onBackPressed in Initial Activity

override fun onBackPressed() {
        super.onBackPressed()
        exitProcess(0)
    }

CodePudding user response:

I think you should make a separate activity for splash and keep the splash activity as the launcher instead of Login activity and and then if the user is not signed in then only go to Loginactivity else start the MainActivity (XTInitActivity in your case)

However if you need to make LoginActivity as the launcher activity then use the following:

override fun onBackPressed(){ super.onBackPressed() exitProcess(0) }

  • Related