0

Show splash screen on fresh start only not on when app is launched while running in background. here's the code of splash activity.

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_intro);

    Handler handler = new Handler();
    handler.postDelayed(() -> {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    },2000);

}

}

  • Does this answer your question? [How to show splash screen when app is in background in Android?](https://stackoverflow.com/questions/43238648/how-to-show-splash-screen-when-app-is-in-background-in-android) – Lukas Novicky May 11 '20 at 05:28
  • I already saw that question before, that's not what i want. –  May 11 '20 at 05:44
  • I'd advise _against_ this code in general, you're just adding two seconds of "lag" to your app's startup time for no reason, which will probably irritate your users. I'd strongly recommend something like [this](https://stackoverflow.com/a/15832037/208273) instead. – Ryan M May 12 '20 at 00:29

2 Answers2

0

Create new activity for show splash screen. And then in your AndroidManifest.xml move

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

from MainActivity to your splash activity tag

  • Thanks but it's gonna show splash screen everytime when app is launched, I want to avoid splash screen when app is launched while running in background. Any solution? –  May 11 '20 at 05:19
0

Normally an app that runs in the background resumes to its previously access activity/fragment when called to the front. If you run into your splash screen whenever it resumes, you haven't properly managed your activities lifecycle.

tariku tsegaye
  • 101
  • 1
  • 2
  • I added the code of splash activity. Check if you find something wrong. thanks –  May 11 '20 at 05:40