Day 6 – Learning Android Development

The internet was cut off today for unknown reasons so what I did was use my phone’s 3G network to continue learning, but I didn’t watch video tutorials because that would’ve been a bit expensive. Instead I played around with eclipse and searched a bit on the Java documentation.

I played around with creating an App from scratch again without the help of tutorials. Create new project, create the starting activity class and its layout, and then linking other activities to the starting one. But I had one problem, how could the code for telling the program to sleep for 1.5 seconds be so long? take a look:

//This is inside the onCreate method of the starting activity
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_title);       
Thread thread = new Thread(){
    public void run(){
        try{
            sleep(1500);
            Intent intent = new Intent("com.jp.mess.around.ACTIVITYONE");
            startActivity(intent);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}; 
thread.start();

Why can’t it be just sleep(1500)? instead of all that?  short answer: you have to.

You must define a new thread. I had never heard of threads before 2-3 days ago when I first linked a new activity to the main one. In python, I would just tell the program sleep(seconds) if i had to wait for something. But the thing is, that would stop execution of code, sleep means the computer isn’t processing anything for that amount of seconds. Threads are awesome, they are threads of execution. The Java virtual machine gives you the ability to have multiple threads of execution running simultaneously. This means you can be running an algorithm that searches for prime numbers and at the same time printing out names of fruit, if you would ever do that.

A new thread is needed in my case because if I tell the current (and single) thread to sleep and then open up a new activity, my previous activity wont show the title. That means my previous activity freezes and doesn’t show anything for those 1.5 seconds. So I need a new thread that will do the waiting and then opening of a new activity, so the old thread can load and run the title.

Leave a comment