0

can any on explain why button is getting displayed.

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<Button 
android:layout_below="@+id/tt"
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Simple Button"/>

</RelativeLayout>

MainActivity

public class MainActivity extends Activity {

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

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
             Toast.makeText(getApplicationContext(), "onCreate", Toast.LENGTH_SHORT).show();    
        }
    }, 10000);

    // Looper.loop();
}

}

For above code out put is

enter image description here

For above code out put is (when i uncomment Looper.loop()) enter image description here

can any one explain this. What Looper.loop() is doing to not display the button on ui. Things i know is Looper is what im using is main ui threads Looper.

i know Looper is already running(looping over the messages) and im calling Looper.loop(); I just want to know what is does that button drawing wont happen on the ui thread means does that Looper get reset or removes some message. exactly what happen when we call Looper.loop() on a thread looper when its already looping.

Sush
  • 3,864
  • 2
  • 17
  • 35

1 Answers1

1

Looper.loop(); is an infinite loop so when you're running this off the main UI thread, the code execution basically stops there, preventing the UI from being displayed.

Not sure what you're trying to accomplish, and I'm not at all too familiar with Looper and threads but here are a few articles I read about Looper.

http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/ http://corner.squareup.com/2013/10/android-main-thread-1.html

singularhum
  • 5,072
  • 2
  • 24
  • 32
  • ur post is very relevant to the answer but not completely. i know Looper is already running(looping over the messages) and im calling Looper.loop(); I just want to know what is does that button drawing wont happen on the ui thread means does that Looper get reset or removes some message. exactly what happen when we call Looper.loop() on a thread looper when its already looping. – Sush Apr 14 '14 at 06:48
  • @sush because when you call Looper.loop, you are "blocking" the rest of the code. In other words, you're blocking the main thread from continuing. It won't ever get to onStart and onResume of the activity because the Looper.loop call you made is waiting for messages. The reason you get your Toast message is that you created a "message" to display the Toast then you called Looper.loop and now all it is doing is waiting and will pick up your Toast to display after 10 seconds and then keeps on waiting. – singularhum Apr 14 '14 at 23:44
  • @sush so one way to visualize this is add an onResume override in your activity and put a debug breakpoint in it. When you debug your app, you will never hit the onResume, as the code execution is still on your Looper.loop line. – singularhum Apr 14 '14 at 23:47