Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change Activity after few seconds

Tags:

android

I need some help with my first Android project. I want to write a app which is showing you a picture with a ImageView for a few seconds I would say so about 4 seconds and after that it change to a second activity which shows a button(only for testing).

My Problem is that my app after I started it in my AVD jump over the picture and shows immediately the button.

How can I fix it? I looked up so long and tried so many things I hope someone of you have a idea :)

Thanks for helping

Here my Code of my MainActivity:

     package com.example.parkourspots;
 public class MainActivity extends Activity {

private ViewTreeObserver vto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final View myLayout = findViewById(R.id.startscreen);

    vto = myLayout.getViewTreeObserver();



    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){

        @Override
        public void onGlobalLayout(){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Intent intent = new Intent(MainActivity.this, select_activity_class.class);

            startActivity(intent);


    }
}); 


}}
like image 827
earny1989 Avatar asked Jul 14 '14 20:07

earny1989


1 Answers

Check this code.

package com.example.parkourspots;

public class MainActivity extends Activity {
    private static int TIME_OUT = 4000; //Time to launch the another activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View myLayout = findViewById(R.id.startscreen);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(MainActivity.this, ActivityTwo.class);
                startActivity(i);
                finish();
            }
        }, TIME_OUT);
    }
}); 
like image 127
Chefes Avatar answered Nov 11 '22 21:11

Chefes