-2

I am new to Android. I'm trying to develop a calculator, which has a home view(activity) which is displayed as soon as the app is launched for a while and then calculator starts as a new activity. In order to wait a while in home screen I am using Thread.sleep() as shown in code below.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
    TextView loading = (TextView) findViewById(R.id.loading);
    loading.setText("Loading...");
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    startActivity(new Intent(MainActivity.this, second.class));
}

The problem is that loading.setText("Loading...") is not writing anything to TextView until 20 sec if put startActivity() in comments.

rgettman
  • 176,041
  • 30
  • 275
  • 357
akhil
  • 347
  • 1
  • 3
  • 16
  • Why would you want to just delay the entire program by 2 seconds and print the false message 'loading'? You're not 'loading', you're sleeping. – user207421 Jan 07 '14 at 01:24
  • 1
    @EJP my guess is its a splash page. – panini Jan 07 '14 at 01:29
  • This is the best recommendation for you "Read an Android Book" and practice on the fly, with these kind of issues you wont get far, a good understanding of threads/main-thread is key for serious development, and don't get me wrong but I've seen a lot of people dropping Android because they don't take the time to read before implementing, if you really want to do android apps, read an android book before coding... – Martin Cazares Jan 07 '14 at 01:38
  • 1
    @panini Obviously. My point is that if you're going to display 'loading ...' you should *be* loading. Not sleeping. – user207421 Jan 07 '14 at 02:26

3 Answers3

3

By adding a Thread.sleep() call to your onCreate() method, you are blocking the UI thread. Try replacing

try {
    Thread.sleep(20000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
startActivity(new Intent(MainActivity.this, second.class));

with

Runnable launchTask = new Runnable() {

    @Override
    public void run() {
        startActivity(new Intent(MainActivity.this, second.class));
    }
};  
loading.postDelayed(launchTask, 20000);
user2368291
  • 96
  • 1
  • 2
1

you are actually delaying Main UI thread. Use async task to start new thread later.

Sathya
  • 140
  • 3
  • Many Many thanks :), I am new if you don't mine will you please tell me, what is async task and how to implement it. – akhil Jan 07 '14 at 01:19
1

That's because you are putting to sleep the UI thread. Only the UI thread updates the screen.

Look here for examples: http://developer.android.com/reference/android/os/AsyncTask.html

You want the setText() to be executed in a function that is called with the Task's thread. Freeing up the UI thread to process screen writes.

T McKeown
  • 12,971
  • 1
  • 25
  • 32