0

I want to clear previous activity but without start new activity on android.

Usually I use this code when change with cleaning

Intent intent = new Intent(SplashActivity.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

but how if I don't want to change activities? maybe someone can help me..

Aditya Rizqi
  • 63
  • 1
  • 7
  • Don't use splash activity, that is not the recommended way. You can customise the theme for splash activity. In this way you no need to clear the previous activity which is SplashActivity in your case. Let me know if you need code for that. – Parmesh Aug 20 '21 at 15:13
  • @Parmesh sounds good idea. may I see what the code looks like? so I can learn and try:) – Aditya Rizqi Aug 20 '21 at 15:15
  • I have provided the solution for my approach, you can find the full code in my GitHub repository(links are provided in solution). Let me know if you still need any further help. – Parmesh Aug 20 '21 at 15:42
  • What do you mean by "clear previous `Activity`? – David Wasser Aug 23 '21 at 13:43
  • SplashActivity.this.finish(); try this. – S_i_l_e_n_t C_o_d_e_r Sep 01 '21 at 06:45

1 Answers1

1

Step1:Create a drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/darker_gray" />
<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/img_splash" />
</item>

Code link: splash_theme_bg.xml

Step2: Create Style

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_theme_bg</item>
</style>

Code link: styles.xml

Step3: Set style in AndroidManifest.xml

<activity
        android:name=".SplashActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Code link: AndroidManifest.xml

Let me know if you still get stuck anywhere. Happy Coding!

Parmesh
  • 480
  • 4
  • 11