Mohamed Ziham
Mohamed Ziham

Reputation: 58

because of Thread my intent is not starting

What's wrong with the code? I can't find any errors and the splash screen won't get dispose and the intent is not working because of it. I tried a different code for this splash screen it's working without any problem. I just want to know why this code is not working.

public class Splash extends Activity {

    @Override
    protected void onCreate(Bundle sScreen) {
        super.onCreate(sScreen);
        setContentView(R.layout.splash);

        Thread timer = new Thread(){
            public void Run(){
                try{
                    sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }finally {
                    Intent intent = new Intent("com.projects.ziham.learning.STARTINGPOINT");
                    startActivity(intent);
                }
            }
        };
        timer.start();
    }
}

and I changed intent parameters its also not working example:

Intent intent = new Intent(Splash.this,StartingPoint.class);

Upvotes: 0

Views: 139

Answers (6)

MSD
MSD

Reputation: 81

Replace Run into run in your code and also Use Handler.

Thread timer = new Thread(new Runnable() {
    @Override
    public void run() {
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e) {
           e.printStackTrace();

        } finally {
            Intent intent = new Intent(MainActivity.this,SecondPage.class);
            startActivity(intent);
        }
    }


    });
timer.start();

Upvotes: 0

arnab.p
arnab.p

Reputation: 43

Instead of Timer you can use Handler.

new Handler().postDelayed(new Runnable() {
             @Override
             public void run() {
             Intent intent = new Intent(Splash.this, StartingPoint.class);
                    startActivity(intent);
             }
}, 3000);

Upvotes: 0

Ganesh Gudghe
Ganesh Gudghe

Reputation: 1387

Please try the following to redirect after some time delay

 new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                       Intent i = new Intent(SplashScreen.this, yourActivity.class);
                       startActivity(i);
                      finish();
               }
           }, 1000);

Please try this

    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println("run called");
             try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                System.out.println("finally called");
            }
        }
    });
    t.start();

Upvotes: 0

Khalid Taha
Khalid Taha

Reputation: 3313

Either you have to use postDelayed method found in Handler, or you need to run the startActivity in a UIThread:

runOnUIThread(new Runnable(){
    Intent intent = new Intent("com.projects.ziham.learning.STARTINGPOINT");
    startActivity(intent); 
});

Upvotes: 0

Tom Alabaster
Tom Alabaster

Reputation: 975

So from your code, it's a pretty fair assumption to assume this is acting as a splash screen.

While there are now several answers to fix your intent not starting, this article demonstrates the correct way to implement splash screens.

Credit: Big Nerd Ranch

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Try this:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(Splash.this, StartingPoint.class));
    }
}, 1000);

Upvotes: 2

Related Questions