2

Hello I would like to display an other splashscreen after the default one, if the App starts for the first time(right after installation e.g.)

So I wrote this. But the new Activity does not start it stays at the Splash screen. Can somebody say whats wrong with it?

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;

    public class splash extends Activity {
         private Thread splashTread;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            if(!prefs.getBoolean("firstTime", false)) {
                // run your one time code
                 Intent i = new Intent(splash.this, main.class);

                         startActivity(i);
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("firstTime", true);
                editor.commit();


             // thread for displaying the SplashScreen
             splashTread = new Thread() {
                 @Override
                 public void run() {
                     try {
                         synchronized(this){

                                 //wait 2 sec
                                 wait(2000);
                         }

                     } catch(InterruptedException e) {}
                     finally {
                         finish();



                         //start a new activity
                         Intent i = new Intent();
                         i.setClass(splash.this, main.class);
                                 startActivity(i);

                         stop();
                     }
                 }
             };

             splashTread.start();

        }

    }
    }

Thanks.

2
  • Are you sure you're calling setContentView in the activity you're calling?
    – Cruceo
    Jun 14, 2012 at 22:50
  • yes. Thats shouldnt be the problem
    – Ahmad
    Jun 14, 2012 at 22:53

4 Answers 4

6

From what I've seen, your code runs the same Activity (main) regardless of whether the launch is the first time or not. I'm assuming your purpose is to immediately launch the alternate splash screen if it's the first launch, otherwise to proceed to the main Activity after 2 seconds. Also, I would recommend using a Handler rather than a Thread, since you're only making use of it once, and on a delay. Try this:

    public class splash extends Activity
{
    private Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            Intent i = new Intent(splash.this, main.class);
            splash.this.startActivity(i);
                                 this.finish()
        }
    };

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

        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
        {
            this.setContentView(R.layout.splash);
            handler.sendEmptyMessageDelayed(0, 2000);
        }

    }
}
0
1

Instead of calling finish()... just start the main activity with FLAG_ACTIVITY_CLEAR_TOP

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Here's more information on that FLAG

FLAG_ACTIVITY_CLEAR_TOP in Android

To be clearer, what I'm suggesting is that you either

  1. Make the Splash the default activity and in there call the Main activity after a timeout, OR, if the splash has already been seen (preference check). (ie, all splash logic is in the Splash activity)
  2. Have the Main activity check to see if Splash should be called (preference check) and if so start it using the same CLEAR_TOP flag and then have the Splash timeout and set the Main again using the CLEAR_TOP after a couple of seconds. (This mixes the splash logic in both the Splash and the Main)

The end result is that Main will be the only activity on the stack, once the Splash is done.

4
  • That wouldn't actually do anything, as the newly opened Activity would be on top: splash->main. As splash is below main, it would not be closed by this flag.
    – Xono
    Jun 15, 2012 at 8:06
  • using FLAG_ACTIVITY_CLEAR_TOP will make the new activity the ONLY ONE on the stack and it doesn't matter if it was started via Main -> Splash -> Main or Splash -> Main, the end result is that that Main is the only activity on the stack.
    – stuckless
    Jun 15, 2012 at 12:23
  • Uh, no. I suggest you reread the link you posted to android developer: if you have stack A, B, C, D, and have D call startActivity on B's class with that flag, the stack becomes A, B - not just B. The flag only kills activities higher than the started one on the stack, not all others.
    – Xono
    Jun 15, 2012 at 12:36
  • I got the impression from reading his comment (start Splash after Main one) that the Activity order was Main -> Splash, and as a result calling Main again with clear top, should remove Splash and only leave Main. If it goes Splash -> then you are right, the spash would still be around, and the finish() would have to be called after starting the Main (ie, your solution).
    – stuckless
    Jun 15, 2012 at 12:55
0

If your intention is to simply have multiple "Splash" screens, and they have no logic to them, other than just displaying content, then why not just use a single activity and replace the view with a new view for the second splash. You'd have to do this update on the UI thread as well, so you'd need to either use the an Handler or use and AsyncTask, or use View.post() from a known view element in the current view.

-1

I don't think you can/should start activity from a thread that is not the UI thread. Change to an AsyncTask, or use a Handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.