Threading in Android
Threading in Android
WHAT IS THREAD
• A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have
multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are
executed in preference to threads with lower priority.
• In Android, you can categorize all threading components into two basic categories:
• Threads that are attached to an activity/fragment: These threads are tied to the lifecycle of the activity/fragment and
are terminated as soon as the activity/fragment is destroyed.
• LOADERS
• Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. monitor the
source of their data and deliver new results when the content changes. They automatically reconnect to the
last loader's cursor when being recreated after a configuration change.
Loaders are the solution for the problem mentioned above. Loaders can automatically stop when the activity is
destroyed, and can also restart themselves after the activity is recreated.
T H R E A D I N G C O M P O N E N T S T H AT D O N ’ T AT TA C H TO A N A C T I V I T Y / F R A G M E N T
SERVICE
• Service is a component that is useful for performing long (or potentially long) operations without any UI.
• Service runs in the main thread of its hosting process; the service does not create its own thread and does
not run in a separate process unless you specify otherwise.
• A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This
example demonstrate about How to use multiple threads in android.
EXAMPLE
• Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
• Step 2 − Add the following code to res/layout/activity_main.xml.
• <?xml version="1.0" encoding="utf-8"?>
• <LinearLayout 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:orientation="vertical"
• android:gravity="center_horizontal"
• android:layout_marginTop="100dp"
• tools:context=".MainActivity">
<EditText android:textColor="#FFF"
android:id="@+id/edit_query" android:layout_height="wrap_content"
android:layout_width="match_parent" android:text="Button" />
android:layout_height="wrap_content" <TextView
android:hint="Enter string" /> android:id="@+id/text"
<Button android:layout_width="wrap_content"
android:id="@+id/click" android:layout_height="wrap_content" />
android:layout_marginTop="50dp" <TextView
android:id="@+id/text1"
style="@style/Base.TextAppearance.AppCompat.Widge
android:layout_width="wrap_content"
t.Button.Borderless.Colored"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
</LinearLayout>
android:background="#c1c1c1"
• In the above code, we have taken edittext and textview. When user enter some text into edittext, it going to wait till 5000ms and update
both textview’s with thread name.
• Step 3 − Add the following code to src/MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;