Android Activity with Example


What is Android Activity ?

A Single user interface screen in android treat as Activity. Example in the case of user registration and login application there will the different activity one for Login and one for registration at last activity which will display after login in the application.





Activity StateDescription
onCreate()This is the first callback and called when the activity is first created.
onStart()This callback is called when the activity becomes visible to the user.
onResume()This is called when the user starts interacting with the application.
onPause()The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.
onStop()This callback is called when the activity is no longer visible.
onDestroy()This callback is called before the activity is destroyed by the system.
onRestart()This callback is called when the activity restarts after stopping it.


    Every state in activity life cycle is useful and important for work related to their state.

By the following code we understand how these state will use in the code-

---------------------------------------------------------------------------------------------
package com.dheeruapps.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
   String msg = "Android : ";
   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   /** Called when the activity is about to become visible. */
   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   }

   /** Called when the activity has become visible. */
   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   }

   /** Called when another activity is taking focus. */
   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   }

   /** Called when the activity is no longer visible. */
   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   }

   /** Called just before the activity is destroyed. */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}


-------------------------------------------------------------------------------------------

        The activity class loads all the component of the UI that are defined in the xml file in layout folder  name "activity_main.xml".


setContentView(R.layout.activity_main);
 
 

        Every activity is also define in the Android Manifest file which specify the which of the application is used in application.


 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" />
   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name=".MainActivity"
           android:label="@string/title_activity_main" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
   </application>
</manifest> 
 
 
 

 

0 comments:

Post a Comment

My Instagram