tags:

views:

54

answers:

2

Hi all,

My code looks as follows..

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

    try {
        Thread.sleep(3000);
        } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     finish();
    }

And my XML is

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_gravity="center"><Button android:id="@+id/Button01" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="Button"
    android:layout_gravity="center"
    android:layout_marginTop="80px"></Button>

In my XML i had a button. Now what i want is After starting the activity i have to wait for 3 seconds and then the Activity has to be stopped..Through my code the Activity is stopped after 3 seconds. But am not seeing any Button when the activity is started..Only Black screen is appearing for 3 seconds..But i also want to display that button..can any one help with sample code..please...I am need of it..

+2  A: 

That's because you're sleeping in the onCreate, which means it's holding up in the UI thread before actually rendering the screen (which happens after onCreate finishes running). You'll really want to do the sleeping in a separate thread, either with a standard Java Thread/Runnable or an AsyncTask, to prevent users getting an Application Not Responding (ANR) dialog, too. There's a very good Android Developers Blog post about threading on Android that I highly recommend reading if you're at all confused about how threading works in Android activities, especially if you're used to server or other non-UI Java coding.

Yoni Samlan
Thanku...i Got it
A: 

You should not be sleeping for more than a few ms in the UI thread, as the UI thread is user event driven rather than an appropriate place for self-synchronized procedural tasks.

If you really want to do this, set a timer for 3 seconds.

Chris Stratton