1

I have an app with the following structure:

I have a launcher Activity -> MainActivity.kt, which in its onCreate, calls the SplashScreen.kt.

In this SplashScreen.kt, I download data from my server and then finally open Dashboard.kt

In the Dashboard.kt, whenever, I press back twice, I want to quit the application.

This is how I've set up the quit function

private fun quit(){
        if (lastBackPressed + 2000 > System.currentTimeMillis()){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                this.finishAffinity()
            } else{
                this.finish()
                exitProcess(0)
            }
        }
        else Toast.makeText(
            context,
            "Press once again to exit!",
            Toast.LENGTH_SHORT
        ).show()
        lastBackPressed = System.currentTimeMillis()
    }

To do so, I've referenced the following questions:

However, all the solutions there, could only help me to quit my Dashboard.kt

As soon as Dashboard.kt is quit, MainActivity starts again and loads the SplashScreen.kt, which in turn loads the Dashboard.kt (again).

Is there a better and efficient way to quit the app, directly from the Dashboard.kt activity?

SowingFiber
  • 1,194
  • 1
  • 12
  • 32
  • 1
    Are you finishing previous activity before starting new one? – Kaushik Burkule Dec 14 '19 at 06:16
  • Nope. Thanks. That was a silly mistake >/// – SowingFiber Dec 14 '19 at 06:21
  • I like how you only download data in the SplashActivity... because that is not guaranteed to run during the application's execution. – EpicPandaForce Dec 14 '19 at 10:04
  • @EpicPandaForce Interesting! Could you explain that in detail? I'm actually downloading jsonData from my server in the Splash. This data is further used to create the dashboard elements. How is it that is not guaranteed to run? – SowingFiber Dec 14 '19 at 10:20
  • @SowingFiber because Android can skip your splash if you're returning from background. See https://stackoverflow.com/questions/49046773/singleton-object-becomes-null-after-app-is-resumed/49107399#49107399 – EpicPandaForce Dec 14 '19 at 15:16
  • Oh, then there are no issues for my approach. As I save the data in sql if i don't want it to reload during the course of the app's execution. For now, I've set it up such that my data downloads and saves it in SQL in the Splash. Then, I use the saved data from sql to operate in my app. Since, I don't delete my sql data after I've exited the app, I still have the data, even if the app skips the splash. – SowingFiber Dec 16 '19 at 05:23

1 Answers1

2

In the app's manifest file, add android:noHistory="true" flag in the main activity. this will allow your app to open MainActivity and then remove it from the stack whenever another activity is called.

For example in your case:

MainActivity.kt

-> SplashScreen.kt

-> Dashboard.kt (MainActivity.kt remains in the stack.)

If you add android:noHistory="true" flag in the main activity then what happens its as below:

MainActivity.kt

-> SplashScreen.kt (MainActivity.kt will be removed from the stack.)

-> Dashboard.kt

-> Press back twice here and you'll be able to exit the app.

Harshvardhan Joshi
  • 2,855
  • 2
  • 18
  • 31