2

So... I've developed a splash screen which is running successfully. How can I make it run once (and only once)? I'd like to build a registration screen but I only want it to appear for the user once.

Help!

Amani Swann

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import com.nfc.linkingmanager.R;

public class SplashScreen extends Activity {

private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 1000; 

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash_screen);

    Handler handler = new Handler();


    handler.postDelayed(new Runnable() {

        @Override
        public void run() {



            finish();

            if (!mIsBackButtonPressed) {

                Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
           }

        }

    }, SPLASH_DURATION);

}

 @Override
 public void onBackPressed() {
    mIsBackButtonPressed = true;
    super.onBackPressed();

}
}
NoobNinja
  • 123
  • 2
  • 14

5 Answers5

3

Use SharedPreferences to store the fact that the screen has already been displayed; upon start, you check this and if so, you replace the Activity by starting the next one and calling finish() for the splash screen immediately.

class stacker
  • 5,357
  • 2
  • 32
  • 65
  • I've updated my source code to include SharedPreferences but it keeps crashing... any suggestions? LOGCAT & JAVA: http://copytaste.com/b2753 – NoobNinja Mar 18 '13 at 17:01
  • @user2163126 It's really hard to tell from the unformatted texts but the Java source does not make much sense and that also seems to be the exception -- how do you even get this run, it's not even a valid Java program? – class stacker Mar 18 '13 at 17:05
  • It runs fine before adding the shared preferences. – NoobNinja Mar 18 '13 at 17:33
  • UPDATED SOURCE (EASIER TO READ) https://docs.google.com/document/d/1HW7jdyiRdepzwGLbuzCPCuKvvOPkNp0H4asCC4Yi9IU/edit?usp=sharing – NoobNinja Mar 18 '13 at 17:38
  • @user2163126 Thanks for the update. -- There are a lot of things wrong with the code, actually. Do you understand Android's handling of the Activity lifecycle? When do you expect Android to execute code which you place outside of a lifecycle callback? – class stacker Mar 18 '13 at 18:20
  • Yes - but I'm very new to everything. I thought that storing the flag as mentioned in the comment below would execute the new activity if 'SplashFlag' came back as false upon loading the layout which would then trigger the alternate activity instead of the one mentioned below which was including in the single working splash screen I had before. P.S. I understand I called .NewCore 2x, I switched it from the new activity I created back to .NewCore to ensure the force close wasn't a problem w the new activity I created. (Just testing on an activity I know is stable - apologies for the typo!) – NoobNinja Mar 18 '13 at 18:33
  • Should it look more like this? https://docs.google.com/document/d/1duKnieRamjiDhaaQbHrz5pjUzLK90Hui4ZJMSfu4BCs/edit?usp=sharing – NoobNinja Mar 18 '13 at 18:50
  • This is driving me crazy... (I know I'm getting close to finding a solution!) – NoobNinja Mar 18 '13 at 19:01
  • I think I figured it out... but now none of my MenuItems are appearing - would you mind taking a look? http://stackoverflow.com/questions/15489377/splash-screen-causes-menuitems-not-to-appear – NoobNinja Mar 19 '13 at 01:36
1

You need to store a flag in shared preferences like this

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
finish();
if (prefs.getBoolean("SplashFlag", false)&&!mIsBackButtonPressed){

 Intent intent = new Intent(SplashScreen.this, NewCore.class);
                SplashScreen.this.startActivity(intent);
}else {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("SplashFlag", true); // value to store
//again there are values for int, long, string, float, boolean
editor.commit();//This is needed or the edits will not be put into the prefs file
}
Rohit
  • 1,001
  • 1
  • 11
  • 20
  • I've added your code just after my "public class SplashScreen extends Activity {" but I'm getting an error stating "preferences cannot be resolved" ...any suggestions? : ) – NoobNinja Mar 18 '13 at 16:46
  • It's also force closing... (I've included my LOGCAT and updated java w the SharedPreferences snippet you posted above) http://copytaste.com/b2753 – NoobNinja Mar 18 '13 at 16:58
  • I think I figured it out... but now none of my MenuItems are appearing - would you mind taking a look? http://stackoverflow.com/questions/15489377/splash-screen-causes-menuitems-not-to-appear – NoobNinja Mar 19 '13 at 01:38
0

Use SharedPreferences where you could store a boolean for instance seenSplash which you could default false. Then use an if that checks if it's false and then shows the splash. After a user has seen the splashscreen get an Editor where you change that boolean to true.

Don't forget to commit() the change on the editor.

David Olsson
  • 8,085
  • 3
  • 30
  • 38
  • I tried this... but my app is force closing. If you'd like to take a look I've included a copy of my LOGCAT and my updated JAVA http://copytaste.com/b2753 – NoobNinja Mar 18 '13 at 17:05
  • Sorry, but your code is a mess with that copytaste. Take a look at SplashScreen.java:16. There's several examples on the internet with SplashScreens and SharedPreferences. I guess you have an NPE for not declaring them correctly (guess, not based on the code or logcat) – David Olsson Mar 18 '13 at 18:57
  • Do you know of a good one? I just googled around a bit and didn't find any. – NoobNinja Mar 18 '13 at 19:58
  • Look into your crash, google that reason and you will most likely find the solution. It was a long time ago I looked into that myself. Now I just write it myself and no, I really don't want to do someone elses job. – David Olsson Mar 18 '13 at 20:35
0

in your register screen get login details and compare with values which you have saved in shared preferences. or hard-code like username is admin and password is admin. is both values are true just to enter splash screen otherwise redirect to other page whatever you like.

  • Hi! I'm still having trouble with this... I've added the code suggested above but it still fails. (Any suggestions?) JAVA: https://docs.google.com/document/d/1NHYEnkOjApa9RtTVCF6AJvmry6nFjZivNA0nnQ1oifQ/edit?usp=sharing – NoobNinja Mar 18 '13 at 20:28
  • This is by far not the best way of achieving this, most splash screens load prior to login screens so by the time it gets to the login screen it is to late. – auracool Oct 28 '14 at 14:55
0

There are two ways to do that

1st

finish();
SplashScreen.this.startActivity(intent);

2nd in Your Android Manifest where you declared your splash screen activity

add this

android:noHistory=true
Rohit
  • 1,001
  • 1
  • 11
  • 20
  • I think I have a pretty different understanding of what the original poster actually asked. – class stacker Mar 18 '13 at 15:10
  • I tried this (it seemed simple and straightforward) but it's not working. It still loads the same way it did before. ANDROID MANIFEST: http://copytaste.com/t2752 SPLASH SCREEN JAVA (w your suggested changes) http://copytaste.com/w2752 – NoobNinja Mar 18 '13 at 15:26
  • Any suggestions? (I'm really not sure how I would go about using the other suggested methods. I'm pretty new to android development) – NoobNinja Mar 18 '13 at 15:41
  • @user2163126 What this suggestion achieves (in both variants) is that your splash screen will not appear in the Activity history. It is totally unrelated to your question if you ask me. – class stacker Mar 18 '13 at 15:53
  • Ok - I'm watching an Android tutorial on SharedPreferences - I'll get back to you after I figure out how to implement SharedPreferences into my source code. http://www.youtube.com/watch?v=UC2wAuxECw0 – NoobNinja Mar 18 '13 at 16:01
  • Hi! I'm still having trouble with this... I've added the code suggested above but it still fails. (Any suggestions?) JAVA: https://docs.google.com/document/d/1NHYEnkOjApa9RtTVCF6AJvmry6nFjZivNA0nnQ1oifQ/edit?usp=sharing – NoobNinja Mar 18 '13 at 20:29