Android Activities

Android Activities

Android Activities this tutorial is all about this word. I will clarify you what is  android activities and what its life-cycle. so let starts, An android activities shows a single screen with a UI (user interface) exactly like window of Java.Android activity is the subclass of ContextThemeWrapper class.

In This Android activities Tutorial we will clarify you What is Android Activities with Simple And Helpful Examples. Let’s Start

In the event that you have worked with C, C++ or Java programming language then you more likely than not seen that your program begins from main() function. Fundamentally the same as way, Android framework starts its program with in an Activity beginning with an call onCreate() callback method. There is a grouping of callback techniques or methods that start up an activity and an arrangement of callback methods that tear down an activity as appeared in the beneath Activity life cycle graph: (picture couresy : android.com )

android activities

The Android Activities class characterizes the following call backs i.e. occasions. You don’t have to implement every one of the callbacks methods. In any case, it’s essential that you see every one and execute those that guarantee your application behaves on the way clients expect.

         Callback             Description

  • onCreate()          This is the first callback and called when the activity is initially made.
  • onStart()              This callback is called when the activity gets to be visible to the client.
  • onResume()       This is called when the client begins interfacing with the application.
  • onPause()           The pause activity does not get client input and can’t execute any code and called when the present action is being delayed and the past action is being continued.
  • onStop()              This callback is called when the activity is no more noticeable.
  • onDestroy()       This callback is called before the action is destroyed by the framework.
  • onRestart()         This callback fuction  is called when the activity restarts after stopping it

Android activities Example

This case will find a way to show Android activities life cycle. Following are the steps to change the Android application we made in Hello World Example section: Follow these steps

 Steps            Description

1.     You will utilize eclipse IDE to make an Android application and name it as HelloWorld under a package com.example.helloworld as clarified in the Hello World Example tutorial.

2.    Modify main activity file MainActivity.java as clarified underneath. Keep rest of the files unaltered.

3.     Run the application to launch Android emulator and check the consequence of the  progressions done in the application.

Content of the modified main activity file is following src/com.example.helloworld/MainActivity.java.  This file incorporates each of the basic life cycle methods. The Log.d() method has been utilized to create log messages:

package com.example.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");
   }
}

 

An android  activities class stacks all the User Interface part utilizing the XML file accessible as a part of res/layout folder of the project. Taking after statement  loads User interface  parts from res/layout/activity_main.xml file:

setContentView(R.layout.activity_main);

 

An app can have one or more android activities with no confinements. Each activity you characterize for your application must be pronounced in your AndroidManifest.xml file and the main activity for your application must be proclaimed in the show with a <intent-filter> that includes the MAIN action and LAUNCHER class as given below:

<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="22" />
   
   <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>

 

In the event that either the MAIN action or LAUNCHER class are not pronounced for one of your android activities, then your application symbol won’t show up in the Home screen’s list of applications.

We should attempt to run our changed Hello World! application we simply changed. I accept you had made your AVD while doing Anrdoid Environment setup. To run the application from Eclipse, open one of your project’s activity files and click on  Run symbol from the toolbar. Eclipse installs the application on your AVD and begins it and if all is well with your setup and application, it will show Emulator window and you should to see log messages (given below)in LogCat window in Eclipse IDE:

07-19 15:00:43.405: D/Android :(866): The onCreate() event
07-19 15:00:43.405: D/Android :(866): The onStart() event
07-19 15:00:43.415: D/Android :(866): The onResume() event

 

android activities

Now we click Red button on Android Emulator and it will generate events messages which stores in LogCat window in Eclipse

07-19 15:01:10.995: D/Android :(866): The onPause() event
07-19 15:01:12.705: D/Android :(866): The onStop() event

 

we again click on Menu button  on Android Emulator and it will produce the following events messages stores in LogCat window in Eclipse IDE:

07-19 15:01:13.995: D/Android :(866): The onStart() event
07-19 15:01:14.705: D/Android :(866): The onResume() event

After that we again click on Back button  on android emulator and it will produce the following events messages in  LogCat window in Eclipse IDE and this step completed the Activity Life Cycle for an Android App


07-19 15:33:15.687: D/Android :(992): The onPause() event
07-19 15:33:15.525: D/Android :(992): The onStop() event
07-19 15:33:15.525: D/Android :(992): The onDestroy() event