Thursday, 23 April 2015

Android Activity Lifecycle

Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.
An activity is the single screen in android. It is like window or frame of Java.
By the help of activity, you can place all your UI components or widgets in a single screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.

Android Activity Lifecycle methods

Let's see the 7 lifecycle methods of android activity.
MethodDescription
onCreatecalled when activity is first created.
onStartcalled when activity is becoming visible to the user.
onResumecalled when activity will start interacting with the user.
onPausecalled when activity is not visible to the user.
onStopcalled when activity is no longer visible to the user.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed.
android activity lifecycle

Android Activity Lifecycle Example

It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.
File: MainActivity.java
  1. package com.example.activitylifecycle;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.util.Log;  
  5. import android.view.Menu;  
  6. public class MainActivity extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         Log.d("lifecycle","onCreate invoked");  
  12.     }  
  13.     @Override  
  14.     protected void onStart() {  
  15.         super.onStart();  
  16.          Log.d("lifecycle","onStart invoked");  
  17.     }  
  18.     @Override  
  19.     protected void onResume() {  
  20.         super.onResume();  
  21.          Log.d("lifecycle","onResume invoked");  
  22.     }  
  23.     @Override  
  24.     protected void onPause() {  
  25.         super.onPause();  
  26.          Log.d("lifecycle","onPause invoked");  
  27.     }  
  28.     @Override  
  29.     protected void onStop() {  
  30.         super.onStop();  
  31.          Log.d("lifecycle","onStop invoked");  
  32.     }  
  33.        @Override  
  34.     protected void onRestart() {  
  35.         super.onRestart();  
  36.          Log.d("lifecycle","onRestart invoked");  
  37.     }     
  38.     @Override  
  39.     protected void onDestroy() {  
  40.         super.onDestroy();  
  41.          Log.d("lifecycle","onDestroy invoked");  
  42.     }  
  43. }  


Output:

You will not see any output on the emulator or device. You need to open logcat.
android activity life cycle example output 1
Now see on the logcat: onCreate, onStart and onResume methods are invoked.
android activity life cycle example output 2
Now click on the HOME Button. You will see onPause method is invoked.
android activity life cycle example output 3
After a while, you will see onStop method is invoked.
android activity life cycle example output 4
Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
android activity life cycle example output 5
Now click on the lifecycleactivity icon.
android activity life cycle example output 6
Now see on the logcat: onRestart, onStart and onResume methods are invoked.
android activity life cycle example output 7
If you see the emulator, application is started again.
android activity life cycle example output 8
Now click on the back button. Now you will see onPause methods is invoked.
android activity life cycle example output 9
After a while, you will see onStop and onDestroy methods are invoked.
android activity life cycle example output 10

The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.

Android Intent Tutorial

android intent
Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.
It is generally used with startActivity() method to invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
The LabeledIntent is the subclass of android.content.Intent class.
Android intents are mainly used to:
  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.

Types of Android Intents

There are two types of intents in android: implicit and explicit.

1) Implicit Intent

Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
  1. Intent intent=new Intent(Intent.ACTION_VIEW);  
  2. intent.setData(Uri.parse("http://www.javatpoint.com"));  
  3. startActivity(intent);  

2) Explicit Intent

Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
  1. Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
  2. startActivity(i);  
To get the full code of explicit intent, visit the next page.

Android Implicit Intent Example

Let's see the simple example of implicit intent that displays a web page.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <EditText  
  7.         android:id="@+id/editText1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginTop="44dp"  
  13.         android:ems="10" />  
  14.     <Button  
  15.         android:id="@+id/button1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_below="@+id/editText1"  
  19.         android:layout_centerHorizontal="true"  
  20.         android:layout_marginTop="54dp"  
  21.         android:text="Visit" />  
  22. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package org.sssit.implicitintent;  
  2. import android.net.Uri;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. public class MainActivity extends Activity {  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         final EditText editText1=(EditText)findViewById(R.id.editText1);  
  16.         Button button1=(Button)findViewById(R.id.button1);  
  17.         button1.setOnClickListener(new OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View arg0) {  
  20.                 String url=editText1.getText().toString();  
  21.                 Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));  
  22.                 startActivity(intent);  
  23.             }  
  24.         });  
  25.     }  
  26. }  


Output:

android implicit intent example output 1 android implicit intent example output 2 android implicit intent example output 3

Android Explicit Intent Example

Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity in android by explicit intent.
We can also pass the information from one activity to another using explicit intent.
Here, we are going to see an example to call one activity from another and vice-versa.

Android calling one activity from another activity example

Let's see the simple example of android explicit example that calls one activity from another and vice versa.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/Button01"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/TextView01"  
  13.         android:layout_marginLeft="65dp"  
  14.         android:layout_marginTop="38dp"  
  15.         android:onClick="onClick"  
  16.         android:text="Call second activity" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/TextView01"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignLeft="@+id/Button01"  
  23.         android:layout_alignParentTop="true"  
  24.         android:layout_marginLeft="18dp"  
  25.         android:layout_marginTop="27dp"  
  26.         android:minHeight="60dip"  
  27.         android:text="First Activity"  
  28.         android:textSize="20sp" />  
  29.   
  30. </RelativeLayout>  

activitytwo_main.xml

File: activitytwo_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/Button01"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/TextView01"  
  13.         android:layout_marginLeft="65dp"  
  14.         android:layout_marginTop="38dp"  
  15.         android:onClick="onClick"  
  16.         android:text="Call First activity" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/TextView01"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignLeft="@+id/Button01"  
  23.         android:layout_alignParentTop="true"  
  24.         android:layout_marginLeft="18dp"  
  25.         android:layout_marginTop="27dp"  
  26.         android:minHeight="60dip"  
  27.         android:text="Second Activity"  
  28.         android:textSize="20sp" />  
  29.   
  30. </RelativeLayout>  

ActivityOne class

File: MainActivityOne.java
  1. package com.example.explicitintent2;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.Toast;  
  10. public class ActivityOne extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.       @Override  
  13.       public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         Button button1=(Button)findViewById(R.id.Button01);  
  17.           
  18.         button1.setOnClickListener(new OnClickListener(){  
  19.          public void onClick(View view) {  
  20.           Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
  21.           i.putExtra("Value1", "Android By Javatpoint");  
  22.           i.putExtra("Value2", "Simple Tutorial");  
  23.           // Set the request code to any code you like, you can identify the  
  24.           // callback via this code  
  25.           startActivity(i);  
  26.          }  
  27.          });  
  28.       }  
  29.     }   

ActivityTwo class

File: MainActivityTwo.java
  1. package com.example.explicitintent2;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11. public class ActivityTwo extends Activity {  
  12. /** Called when the activity is first created. */  
  13.   @Override  
  14.   public void onCreate(Bundle bundle) {  
  15.     super.onCreate(bundle);  
  16.     TextView tv=new TextView(this);  
  17.     tv.setText("second activity");  
  18.     setContentView(R.layout.activity_two);  
  19.     Bundle extras = getIntent().getExtras();  
  20.     String value1 = extras.getString("Value1");  
  21.     String value2 = extras.getString("Value2");  
  22.     Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+  
  23.          "\n Second Value: "+value2,Toast.LENGTH_LONG).show();  
  24.     Button button1=(Button)findViewById(R.id.Button01);  
  25.     button1.setOnClickListener(new OnClickListener(){  
  26.         public void onClick(View view) {  
  27.             Intent i = new Intent(getApplicationContext(), ActivityOne.class);  
  28.             startActivity(i);  
  29.           }  
  30.     });  
  31.   }  
  32. }   


Output:

android explicit intent example output 1 android explicit intent example output 2 android explicit intent example output 3


No comments:

Post a Comment