Android splashscreen backstack

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.

Answers


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.


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


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.


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);

}

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.


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

Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish ();

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
    }

Need Your Help

Grep Search Specific Character Trouble

regex linux grep

I have searched extensively and cannot figure out what I am doing wrong here. I have a text file that may contain a string similar to the following:

Enclosing multiple LaTeX documents

latex append tableofcontents

I am not sure if this question is suitable for superuser instead, but I'll ask anyway.