1

I am implementing a splashscreen for a android application. I only want to show the splashcreen once when the app is newly started. After i've done some work I want to go on to the app. If the user then presses the back button I don't want it to return to the splashscreen, I just want the app to exit. How can I implement this in the best way? How do I clear the backstack of the first activity.

AlexanderNajafi
  • 1,631
  • 2
  • 25
  • 39

7 Answers7

2

If you want to show splash screen only for first time when app launches, then you can use above solution of share preferences. But i guess you want to go through following scenarios:

  1. You start the app you get splash screen.
  2. Then you navigate though the app.
  3. Then you come to home screen of you app.
  4. Then you want to exit but splash screen comes.

If you are having this problem then you need to finish splash screen when you start home activity and also at the end you need to logout or finish the app home activity. Also try android:launchMode="singleTask" in splash screen activity tab in android manifest.

Alex Facciorusso
  • 2,290
  • 3
  • 21
  • 34
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
0

When you are going to the second activity after the splash screen, call finish()

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (preferences.getBoolean("firstTime", true)) {
    // Show splash screen
    // Wait a few seconds.
} else {
    // Nothing to do here. Go straight to the second activity.
}
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(
            getSupportActivity()).edit();
editor.putBoolean("firstTime", false);
editor.commit();
startActivity(MainActivity.this, ...)
finish();

This way when the user presses back, there wont be any activity in the stack.

Aswin Rajendiran
  • 3,399
  • 1
  • 21
  • 18
0

Use shared preeferences in android and store the value 1st time.. from second time check if value present dont display splash screen

Edit for preferences follow this

Community
  • 1
  • 1
pavanmvn
  • 749
  • 10
  • 21
0

Hi, try this code it will help you but it will show the splash whenever you open the app.

public class spash_scr extends Activity {

ImageView t;
//LoginButton b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.spash_scr);
    // bm1=drawable.shineme;

    t = (ImageView) findViewById(R.id.textView1);
    t.setImageResource(R.drawable.shineme);

    RotateAnimation r = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);
    // r.setStartOffset(1000);
    r.setDuration(2000);
    r.setFillAfter(true);
    t.startAnimation(r);

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Intent i = new Intent(spash_scr.this, MainActivity.class);
            startActivity(i);
            finish();

        }
    }, 3000);

}
Mohan
  • 311
  • 8
  • 15
0

You weren't clear in the exact behavior, so here are some options:

A: You want it to show the splash activity every time the task (app) is restarted, such as after phone reboots, user's manual task closing, or when Android drops it for memory reasons. (This is usually used for branding or licensing logos.) In those cases, launch the splash from the main activity's onCreate(), then Finish() the splash screen to allow the user to return to the main view. That way navigating back won't bring the splash activity back, since it is not in the navigation stack anymore.

B: You want it to show the splash screen the first time the app is launched after installation, but never again. (Usually used for 'welcome' or 'get started' help views.) Use a SharedPreference setting as described in other answers here, or in the Using Shared Preferences documentation. In the case it should show the splash, I still suggest option A for the simplest way to not show the splash screen again for after it is first dismissed after the first launch.

C: An even more, unknown complex navigation? Learn about Tasks and Back Stack and you can make it do pretty much whatever you want.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
0

After opening the new Activity this.finish (); You should do

Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish ();
g89n7man
  • 59
  • 1
  • 1
  • 6
-1

For that you should use SharedPreferences ,

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if(!prefs.getBoolean("first_time", false))
    {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("first_time", true);
        editor.commit();
        Intent i = new Intent(splash.this, otherSplash.class);
        this.startActivity(i);
        this.finish();
    }
    else
                // Not firsttime Direct it as you wish
    }
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64