Home > other >  How can I back to before 2 screen Android?
How can I back to before 2 screen Android?

Time:03-18

This is the scenario: I've got 3 pages; the first page A is the main page and uses splash, the second page is the B page, the third page is the C page. I want to navigate from C to A but I do not want to see the splash. I don't want to see splash but when I open page A splash always comes. I don't want to delete splash.

I try some code e.g. :

1-Intent goScreen = new Intent(C.this or this@C, A.class); goScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(gotoScreenVar);

2-val intent = Intent(this@A, C::class.java) startActivity(intent)

CodePudding user response:

While navigating from Activity C to Activity A, you can pass some data to the intent. Like

intent.putBoolean("shouldHideSplash", true);

and on Activity A, you can check if intent has shouldHideSplash, you can skip the splash and can do whatever you want.

CodePudding user response:

Create this extension property:

val Activity.isLaunchedFromHomeScreen: Boolean
    get() = intent?.let {
        it.action == Intent.ACTION_MAIN &&
                it.categories.orEmpty().contains(Intent.CATEGORY_LAUNCHER)
    } ?: false

Then in Activity A, you can check this Boolean to decide whether to show the splash.

But I caution you about creating a splash screen this way. Now that Android 12 has built-in splash screens, your app is going to show two splash screens on Android 12 and higher. You might want to use the splash screen library to create one that works consistently across all versions of Android. Or create another property to decide to show your implementation only on versions lower than Android 11:

val shouldShowSplashScreen: Boolean 
    get() = isLaunchedFromHomeScreen && Build.VERSION.SDK_INT < Build.VERSION_CODES.R
  • Related