Never I thought I will break up so easily.
But I have.
The burden of life becomes too much.
Where to go.......Nowhere.
Whom to talk .....Noone.
What to do .......Nothing.
package com.example.chetannrathore.testapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView tv; Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv =(TextView) findViewById(R.id.textView); bt=(Button) findViewById(R.id.button); tv.setText("before"); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tv.setText("inbetween"); } }); tv.setText("after"); } }
Now when I run the application, on screen I am seeing AFTER being displayed. Now basically CODE always
get executed sequentially.
So after tv.setText("before") ; bt.setonClickListener should get executed.
Now I was thinking that till we have not pressed the button, the code should be stopped at this point. But it
doesn't happens.
What happens is ...tv,setTEXT("after") also get executed.
So on my screen I can see after even when button is not pressed.
So I thought that button click listener will be running on separate thread. But that is totally wrong.
On reading various post, this is what happens.
Every activity runs on single THREAD called MAIN thread and also UI thread.
So even when you have multiple screen application, all of them run on same MAIN thread.
--- This needs to be taken care while designing the APPLICATION, else it will be slow --------
Coming back to code, all the UI components like button,list,textbox also known as widget are part of
VIEW CLASS.
So when we define button in our application, we are creating a class of it ( which actually android does for us)
We just need to find it and assign it correctly.
bt=(Button) findViewById(R.id.button);
Now like a normal class, WIDGET CLASS also have methods and interface defined in them.
For example in case of BUTTON , we have setOnclickListener method. This method needs a View object ,
which implements View.OnCLICKListener interface.
Hence we do
bt.setOnClickListener(new View.OnClickListener() {
Now if we use interface, we have to implement its methods....in case of onClickListener , that method is
OnClick hence we write code for that.
Also , we need to know which button is clicked, hence OnClick has one parameter which is View.
public void onClick(View v) {
Now OnClickListener is part of EventListener Interface in Android. These EventListener
has callbacks method , for example Onclick.for OnClickListener Interface
Android framework registers these eventlistener interface callbacks. And wheneven a button or
scrollview or textview is clicked or focused upon, it calls the respective callback function and also
passes the VIEW object which is clicked.
In conclusion , whenever we are running Android application ,
APPLICATION MAIN THREAD is running and also ANDROID FRAMEWORK always be running.
If no button is clicked , MAIN THREAD will suspend.
When button is clicked, FRAMEWORK will call the eventlistener interface function, which will put MAIN THREAD
from SUSPENDED state to ACTIVE state.
Once the button functionality is done, again MAIN THREAD will move to SUSPENDED state.
printStream.print(msg); // Finally we have send message}
String x = "";
x = r.readLine(); // So now in x we have our input data, which we can display
}class
ClientTask
extends
AsyncTask {
printStream.print("data to be send in string format");