+ Reply to Thread
Results 1 to 2 of 2

Thread: How to stop an Activity in android

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    How to stop an Activity in android

    My code looks as follows..
    Code:
    @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
    Code:
    <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 ?

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    14
    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.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts