Sunday, 8 December 2013

Android Life Cycle of Activity


What is Activity?

 Activities are our application’s presentation layer. The UI of  your application is built around
one or more extensions of the Activity class. Activities use Fragments and Views to layout
and display information, and to respond to user actions. Compared to desktop development,
Activities are equivalent to Forms.

Life Cycle methods of Activity are used to control and manage the resource.
Following methods are available android.app.Activity class.
  •   OnCreate: called when activity is first created.
  •  OnStart: called when activity is becoming visible to the user.
  •   OnResume: called when activity will start interacting with the user.
  •    OnPause: called when activity is not visible to the user.
  •   OnStop: called when activity is no longer visible to the user.
  •   OnRestart: called after your activity is stopped, prior to start.
  •   OnDestroy: called before the activity is destroyed.


Life Cycle Activity XML file:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Activity_life_cycle" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="106dp"
        android:layout_marginTop="110dp"
        android:text="Finish" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="58dp"
        android:ems="10" />

</RelativeLayout>

Life Cycle Activity .Java File:

package com.solomon.lifecycleactivity;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Activity_life_cycle extends Activity {

public static final String TAG="LifeCycleActivity";
Button finishButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_life_cycle);
finishButton = (Button)findViewById(R.id.button1);
finishButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
finish();
/*When we click this button our program will end and
                                    onPause,OnStop,OnDestroy methods will be called.*/
}
});
 Toast.makeText(getApplicationContext(),"Oncreate",Toast.LENGTH_LONG).show();

//Using Toast we can display some Pop-up messages.
        /*In this program whenever oncreate or other methods are called
                    corresponding Toast message will appear on Emulator.*/
Log.d(TAG,"In Oncreate");

//This string will be displayed  in LogCat.
}

@Override
protected void onDestroy() {
   super.onDestroy();
  Toast.makeText(getApplicationContext(),"OnDestroy",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnDestroy");
}


@Override
protected void onPause() {
   super.onPause();
  Toast.makeText(getApplicationContext(),"OnPause",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnPause");
}


@Override
protected void onRestart() {
     super.onRestart();
     Toast.makeText(getApplicationContext(),"OnRestart",Toast.LENGTH_LONG).show();
      Log.d(TAG,"In OnRestart");
}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
 super.onRestoreInstanceState(savedInstanceState);
   Toast.makeText(getApplicationContext(),"OnRestoreInstanceState",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnRestoreInstanceState");
         // called when the activity is started after it is destroyed by the system.
}

@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"OnResume",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnResume");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

/* When user change orientation of mobile device or Emulator
     this method is called. Current state of Activity is saved when this 
                     method is called. By Pressing Ctrl+F11 we can change the 
                     orientation of Emulator.  
*/
 Toast.makeText(getApplicationContext(),"OnSaveInstanceState",
         Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnSaveInstanceState");
}

@Override
protected void onStart() {
      super.onStart();
     Toast.makeText(getApplicationContext(),"OnStart",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnStart");
}
@Override
protected void onStop() {
  super.onStop();
  Toast.makeText(getApplicationContext(),"OnStop",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnStop");
}

}

AndroidManifest.Xml File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solomon.lifecycleactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solomon.lifecycleactivity.Activity_life_cycle"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Output:








//Like this for every methods of life cycle Activity ,Toast message will appear.

LogCat Message:







   /*If we change the orientation of mobile or Emulator
       OnSaveInstanceState and OnRestoreInstanceState will be called.*/
















































































Saturday, 7 December 2013

Hello World Example

Hello World Example:

1.Layout XML File:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HelloWorldExample_MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="26dp"
        android:text="Enter Name" />

    <EditText
        android:id="@+id/name_editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name_editText1"
        android:layout_below="@+id/name_editText1"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="30dp"
        android:onClick="welcome"
        android:text="Submit" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="28dp"
        android:text="Welcome" />

</RelativeLayout>
  
   After writing this much code you will get Following UI screen:
   // We dont have to write everything by ourself ,we can do just drag and drop from Palette.


2. .JAVA file for our Activity :

package com.solomon.HelloWorldExample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class HelloWorldExample_MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_world_example__main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hello_world_example__main, menu);
return true;
}
public void welcome(View view) {
//While clicking button1 this welcome method will call of onclick.
String name;
TextView welcomeTextView;
EditText nameEditText;
nameEditText=(EditText)findViewById(R.id.name_editText1);

//Using EditText we can get values from user
//Here,I am getting a name and storing in nameEditText

welcomeTextView=(TextView)findViewById(R.id.textView2);
//TextView is more like a Label.

name=nameEditText.getText().toString();
//Getting Text from EditText and storing it in name(local variable).

welcomeTextView.setText("Hello"+name+".Welcome to Android World");
//whatever value stored in name is added with 2 more string and stored in WelcomeTextView.
}

}

3.AndroidMainfest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solomon.HelloWorldExample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solomon.HelloWorldExample.HelloWorldExample_MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.Output: