Activity and Activity Life Cycle in Android


What is Activity?

Activity is a component and this is a single screen for user interference,
In any Application can be multiple activity according to depend on user screens in application and its compulsory to define all activity on AndroidManifest.xml file other wise we will get an error ClassNotFindException and also we have to declear the starting point of application with the help of defile launch activity in AndroidManifest.xml file by use of <intent-filter> that includes the MAIN action as show an example below.
Activity has own life cycle and we invoke the method  according to requirement .the starting point of Activity is onCreate() method. now we will discuss about all collection  callback methods of activity lifestyle.

Activity LifeCycle?

There are 7 life cycle methods of android.app.Activity class.






1) onCreate();

this called When Activity created first time

onStart();

onStar called when activity becoming visible to user. this is called from two times - first after OnCreate method and second after onRestart method. After called onCreate method OnResume call immediately.

onResume();

onResume method invoke when Activity will interact with user. It's followed by onPause method and this method always use for change UI during the period of when Activity not visible.when onResume is running condition at this point Activity is on top of the Activity stack.

onPause();

onPause method is called when the activity into the background or when the activity becomes partially invisible. we have two Activity x and y.  suppose Activity  x is on top of the Activity stack and Activity y is launched in front of activity x, onPause will be invoke on x.

onStop();

onStop Called when activity are no longer visible to user.You will next receive either onRestart(), onDestroy(), this is happened because new activity is being started and the activity is being destroyed.

onRestart();

onRestart:  Called when the activity has been stopped and is restarting again and it's  called only after onStop.

onDestroy();

onDestroy(): called after the Activity getting killed,When the user Enter(press) back button on any Activity so activity gets destroyed and control will return to the previous Activity or it’s destroyed and completely removed from memory,



Example:-


package com.android4point.lifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ActivityLifeCycle extends Activity {

    // this called When Activity created first time 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toast.makeText(LifeCycleActivity.this,"onCreate", Toast.LENGTH_SHORT).show();
    }
// onStar called when activity becoming visible to user.
    @Override
    protected void onStart() {
       // TODO Auto-generated method stub
       super.onStart();
        Toast.makeText(LifeCycleActivity.this,"onStart", Toast.LENGTH_SHORT).show();
    }
// onResume method invoke when Activity will interact with user. 
    @Override
    protected void onResume() {
       // TODO Auto-generated method stub
       super.onResume();
        Toast.makeText(LifeCycleActivity.this,"onResume", Toast.LENGTH_SHORT).show();
    }
//called when the activity becomes partially invisible
    @Override
    protected void onPause() {
       // TODO Auto-generated method stub
       super.onPause();
        Toast.makeText(LifeCycleActivity.this,"onPause", Toast.LENGTH_SHORT).show();
    }
// Called when the activity has been stopped and is restarting again 
    @Override
    protected void onRestart() {
       // TODO Auto-generated method stub
       super.onRestart();
        Toast.makeText(LifeCycleActivity.this,"onRestart", Toast.LENGTH_SHORT).show();
    }
//onStop Called when activity are no longer visible to user
    @Override
    protected void onStop() {
       // TODO Auto-generated method stub
       super.onStop();
        Toast.makeText(LifeCycleActivity.this,"onStop", Toast.LENGTH_SHORT).show();
    }
//called after the Activity getting killed.
    @Override
    protected void onDestroy() {
       // TODO Auto-generated method stub
       super.onDestroy();
        Toast.makeText(LifeCycleActivity.this,"onDestroy", Toast.LENGTH_SHORT).show();
    }
}


Thanks


Android Broadcast Receivers


1. Overview 

Now friends we are going to learn about  Broadcast Receivers
Broadcast Receivers is an android componant which response to broadcast messages from an applications or system itself.Broadcast Receivers will catch specific event from android oprating system, this is happen when wifi on,battery status change,recieved phone calls and cut etc.we can also send Broadcast messages throw intent with the help of "sendBroadcast(intent object)" methos.

there are two way to create Broadcast Receiver

1) static type - registered receiver at AndroidManifest.xml file
2) dynamic type -  registered receiver at dynamically with the help of registered() or unregisterReceiver() methods


2.. How to registered receiver at AndroidManifest.xml file

MyReceiver is a name of Broadcast Receiver

<receiver android:name="MyReceiver">
       <intent-filter>
          <action android:name="com.hello.app"/>
          <category android:name="android.intent.category.DEFAULT"/>
       </intent-filter>
</receiver>

3. Create activity_main.xml layout

In this layout we will take two componant first EditText and second Button.we wiil get some text from EditText and after click on Button send broadcast message

<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=".MainActivity" >
   <EditText
      android:id="@+id/editText1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="44dp"
      android:layout_marginTop="26dp"
      android:ems="10" >
    <requestFocus />
  </EditText>
  <Button 
   android:id="@+id/button1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignLeft="@+id/editText1"
   android:layout_below="@+id/editText1"
   android:layout_marginTop="32dp"
   android:text="Send Signal" />
</RelativeLayout>


4.How to create  Broadcast  sender

send Broadcast message by sendBriadcast method

package com.example.broadcastsenderapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {

EditText et;
Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
          et = (EditText) findViewById(R.id.editText1);
          bt = (Button) findViewById(R.id.button1);
       bt.setOnClickListener(new OnClickListener()
       {
            @Override
            public void onClick(View v)
            {
                   String data = et.getText().toString();
                   Intent in = new Intent("com.hello.app");
                   in.putExtra("data",data);
                   sendBroadcast(in);
            }
       });
    }
    @Override
       public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
        return true;
       }
  }

3.Create Broadcast Receiver

here  receive message by intent

package com.example.broadcastsenderapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent in)
   {
        String data = in.getStringExtra("data");
        Toast.makeText(arg0, data, 2).show();
    }
}


if you have any issue and want to know more about Broadcast receiver please comments on blow comments box.

Thanks..