Shiven
Shiven

Reputation: 206

Splash screen doesn't work

public class tryAnimActivity extends Activity 
{

  /**
   * The thread to process splash screen events
   */
  private Thread mSplashThread;    

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Splash screen view
    setContentView(R.layout.janman);

    final tryAnimActivity sPlashScreen = this;   

    // The thread to wait for splash screen events
    mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    // Wait given period of time or exit on touch
                    wait(5000);
                }
            }
            catch(InterruptedException ex){                    
            }

            finish();

            // Run next activity
            Intent intent = new Intent();
            intent.setClass(sPlashScreen, TabsActivity.class);
            startActivity(intent);
            stop();                    
        }
    };

    mSplashThread.start();        
  }

  /**
   * Processes splash screen touch events
   */
  @Override
  public boolean onTouchEvent(MotionEvent evt)
  {
    if(evt.getAction() == MotionEvent.ACTION_DOWN)
    {
        synchronized(mSplashThread){
            mSplashThread.notifyAll();
        }
    }
    return true;
  }    
} 

whats the problem with this code? On clicking, it crashes down. Also, the next activity does not start after the end of this activity. This is the activity used for displaying splashscreen.

Upvotes: 0

Views: 460

Answers (2)

Fran Verona
Fran Verona

Reputation: 5476

I prefer to use Runnable element instead of Thread. My splash code for the activity below works well. I hope it helps you:

public class SplashScreenActivity extends Activity implements OnTouchListener {

    private int SPLASH_DISPLAY_LENGTH = 3000;

    private Handler splashHandler;
    private Runnable splashRunnable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        findViewById(R.id.splash_img).setOnTouchListener(this);

        splashHandler = new Handler();
        splashRunnable = new Runnable()
        {
            @Override
            public void run()
            {
                SplashScreenActivity.this.finish();
                Intent mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
                startActivity(mainIntent);
            }
        };

        splashHandler.postDelayed(splashRunnable, SPLASH_DISPLAY_LENGTH);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            splashHandler.removeCallbacks(splashRunnable);
            SplashScreenActivity.this.finish();
            Intent mainIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
            startActivity(mainIntent);
        }
        return true;
    }
}

Upvotes: 1

durgesh
durgesh

Reputation: 11

just remove stop() method it will work

Upvotes: 1

Related Questions