Thursday, April 14, 2016

Threads - Android Part -1


Its 00:00 AM , my new favorite time to blog.
Also I am fasting ...NAVRATRI ongoing, and I am very hungry but have to wait till morning.


Now the tittle isn't do justice on what I am going to write. Since it will be series of articles only after that we can have more clear picture of Android Threads.


Basically what happened , I am learning Android and was making Server-Client Chat app. Lot of code I got from google....but as I am working more on it , more questions are pouring in.


Coming straight to topic.  Below is very simple Android code.  It has one button and one textbox.

Also it has only one ACTIVITY i.e one screen, on which our button and textview will be visible.

Now the code I have written like below .


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.

 

No comments:

Post a Comment