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..

  • 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

2 Answers2

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
  • 420
  • 4
  • 10
0

Start an Activity, start it like this:

Intent myIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivityForResult(myIntent, 0);

When you want to close the entire app, do this:

setResult(RESULT_CLOSE_ALL);
finish();

RESULT_CLOSE_ALL is a final global variable with a unique integer to signal you want to close all activities.

Then define every activity's onActivityResult(...) callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode)
    {
    case RESULT_CLOSE_ALL:
        setResult(RESULT_CLOSE_ALL);
        finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

See these threads for other methods as well:

Android: Clear the back stack

Finish all previous activities

S_i_l_e_n_t C_o_d_e_r
  • 2,179
  • 2
  • 7
  • 21