Tag Archives: Android Tutorials

Progress Bar In Android UI Control

Progress  bar is used to show progress of a task.

for example when we are uploading or downloading something from internet, it is better to show the progress of download/upload to the. In Android there is a class call progress dialog that allows you to create a progress bar in order to do this we have to instantiate an object of this class.

device-2015-01-05-215451

 

 

 

 

Syntax is: progressdialog progress= new progressdialog(activity.this);

 

.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"
tools:context=".MainActivity" >

<ProgressBar android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="20dp"
android:indeterminate="false"
android:max="100"
android:minHeight="50dp"
android:minWidth="200dp"
android:progress="1" />

<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_below="@+id/progressBar1"/>

</RelativeLayout>

java file:

package com.coreprogrammers.androidprogressbardemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity 
{
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

progressBar = (ProgressBar) findViewById(R.id.progressBar1);

textView = (TextView) findViewById(R.id.textView1);

// Start long running operation in a background thread
new Thread(new Runnable() 
{
public void run() 
{
while (progressStatus < 100) 
{

progressStatus += 1;
//progressStatus=progressStatus+1;
// Update the progress bar and display the 



try 
{
// code runs in a thread
runOnUiThread(new Runnable() 
{
@Override
public void run() 
{
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
}
catch (final Exception ex)
{
Log.i("---","Exception in thread");
}


try 
{
//Sleep for 200 milliseconds. 
//Just to display the progress slowly
Thread.sleep(200);
} 
catch (InterruptedException e) 
{
e.printStackTrace();
}

}
}


}).start();

}



}

 

Design Layout For Multiple Android Screens

When we need to create different layout for different screen size. Support all screen we need to create following layout:

 

1) Low density Small screens QVGA 240×320 (120dpi):

layout-small-ldpi (240x320)  
layout-small-land-ldpi (320x240)

 

2) Low density Normal screens WVGA400 240×400 (x432) (120dpi):

layout-ldpi  (240 x 400 )
layout-land-ldpi  (400 x 240 )

 

3) Medium density Normal screens HVGA 320×480 (160dpi):

layout-mdpi (320 x 480 )
layout-land-mdpi (480 x 320 )

 

4) Medium density Large screens HVGA 320×480 (160dpi):

layout-large-mdpi (320 x 480 )
layout-large-land-mdpi (480 x 320)

 

5) Galaxy Tab ( 240 dpi ):

layout-large  (600 x 1024) 
layout-large-land  (1024 x 600)

 

6) High density Normal screens WVGA800 480×800 (x854) (240 dpi):

layout-hdpi (480 x 800)
layout-land-hdpi (800 x 480)

 

7) For Screen Size of 720×1280 (320 dpi):

layout-xhdpi (720 x 1280)
layout-land-xhdpi (1280 x 720)

 

8) Xoom (medium density large but 1280×800 res) (160 dpi):

layout-xlarge (800 x 1280)
layout-xlarge-land (1280 x 800)

 

 Also add following code in .manifest file:- 

<supports-screens                                 
    android:smallScreens="true"                    
    android:normalScreens="true"         
    android:largeScreens="true"            
    android:xlargeScreens="true"             
    android:anyDensity="true" />

 

Android Fragments Part-2 (Types Of Fragments)

There are some few subclasses that you might want to extend, instead of the base Fragment class:

DialogFragment
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment.
ListFragment
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events.
PreferenceFragment
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a “settings” activity for your application.

How to implement a Button Click event in Android without onClickListener?

For this what you need to do is, just add a property in your XML. Lets see how this is done.

There is a property in the Button tag named ‘onClick’ which has to be give a name first as shown below.

We have two buttons . On first button onClick we will show a toast and on second button’s onClick we will show a promptbox (alertbox).

<Button
 android:id="@+id/btn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click here"
 android:textSize="30dip"
 android:onClick="MyClickMethod"
 />

 

The Whole XML layout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <Button android:id="@+id/btn"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click here"
 android:textSize="30dip"
 android:onClick="MyClickMethod" />

 <Button android:id="@+id/btn2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Click here for pop up!!"
 android:textSize="30dip"
 android:onClick="ShowPopUp" />

</LinearLayout>

 

Now, lets look at the code. Here, we need to create a method with the same name as that of the name we have given as the property name (Here its MyClickMethod & ShowPopUp).

Within the method you can write what you want to do when the button is clicked. Here in my code, I’ve given a toast to be displayed when it is clicked on first button &  on second button’s onClick we will show a promptbox (alertbox).

 

Here is the code, just have a look.

package com.fundoocode.net;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class ButtonClickActivity extends Activity
{
	 /** Called when the activity is first created. */
	  @Override
	 public void onCreate(Bundle savedInstanceState) 
	  {
	     super.onCreate(savedInstanceState);
	     setContentView(R.layout.main);

	  }

	 public void MyClickMethod(View v)
	 {
	      Toast.makeText(getApplicationContext(),"You clicked the first Button " , Toast.LENGTH_LONG).show();
	 }

	 public void ShowPopUp(View v)
	 {
		 Promptbox("Yahoo!!", "Prompt demo", ButtonClickActivity.this); 
	 }

		public static void Promptbox(String title, String mymessage, Activity activity) 
		{
			try 
			{
				new AlertDialog.Builder(activity).setMessage(mymessage).setTitle(title).setCancelable(false).setNeutralButton(android.R.string.ok,new DialogInterface.OnClickListener() 
		            {

					  public void onClick(DialogInterface dialog, int whichButton) 
					  {

				      }

				    }).show();
			} 
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

}

 

The Out Put Screen looks like this before the button is clicked.

 

After the button is clicked, the screen looks like this.

 

 

Activity Lifecycle

activity_lifecycle1

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity — the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

An activity has essentially four states:

  • If an activity in the foreground of the screen (at the top of the stack), it is active or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

The following diagram shows the important state paths of an Activity. The square rectangles represent callback methods you can implement to perform operations when the Activity moves between states. The colored ovals are major states the Activity can be in.

There are three key loops you may be interested in monitoring within your activity:

  • The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of “global” state in onCreate(), and release all remaining resources in onDestroy(). For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().
  • The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user. Between these two methods you can maintain resources that are needed to show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.
  • The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). During this time the activity is in front of all other activities and interacting with the user. An activity can frequently go between the resumed and paused states — for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered — so the code in these methods should be fairly lightweight.

The entire lifecycle of an activity is defined by the following Activity methods. All of these are hooks that you can override to do appropriate work when the activity changes state. All activities will implement onCreate(Bundle) to do their initial setup; many will also implement onPause() to commit changes to data and otherwise prepare to stop interacting with the user. You should always call up to your superclass when implementing these methods.

 public class Activity extends ApplicationContext
 {
     protected void onCreate(Bundle savedInstanceState);

     protected void onStart();

     protected void onRestart();

     protected void onResume();

     protected void onPause();

     protected void onStop();

     protected void onDestroy();
 }

In general the movement through an activity’s lifecycle looks like this:-

Note the “Killable” column in the above table — for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage. In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Be aware that these semantics will change slightly between applications targeting platforms starting with HONEYCOMB vs. those targeting prior platforms. Starting with Honeycomb, an application is not in the killable state until itsonStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.

For those methods that are not marked as being killable, the activity’s process will not be killed by the system starting from the time the method is called and continuing after it returns. Thus an activity is in the killable state, for example, between after onPause() to the start of onResume().

Method Description Killable?  Next
onCreate() Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one.Always followed by onStart(). NO onStart()
onRestart() Called after your activity has been stopped, prior to it being started again.Always followed by onStart() NO onStart()
onStart() Called when the activity is becoming visible to the user.Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden. NO onResume()or onStop()
onResume() Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.Always followed by onPause(). NO onPause()
onPause() Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user. Pre-HONEYCOMB onResume()oronStop()
onStop() Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away. YES onRestart()oronDestroy()
onDestroy() The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. YES nothing

Configuration Changes

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process ofonPause()onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity’s onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

Now We are going to understand the Activity LifeCycle By an Example :-

We are going to show the LifeCycle Flow Of an Activity by two user define activities Avtivity1 and Avtivity2.

First of all we will create an activity Named Activity1:-

package com.lifecycle.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Activity1 extends Activity 
{
      Button btnCAllActivity2;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);
        Toast.makeText(Activity1.this, “onCreate-Activity1″, Toast.LENGTH_SHORT).show();
            btnCAllActivity2=(Button) findViewById(R.id.btnCAllActivity);
            btnCAllActivity2.setOnClickListener(new OnClickListener() 
            {
                          @Override
                          public void onClick(View v) 
                         {
                                   Intent intent = new Intent(Intent.ACTION_VIEW);
                                  intent.setClassName(Activity1.this, Activity2.class.getName());
                                startActivity(intent);
                        }
            }) ;       
    }
    @Override
    protected void onStart() 
    {
          super.onStart();
         Toast.makeText(Activity1.this, “onStart-Activity1″, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onResume()
    {
            super.onResume();
           Toast.makeText(Activity1.this, “onResume-Activity1″, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onPause() 
    {
              super.onPause();
            Toast.makeText(Activity1.this, “onPause-Activity1″, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onStop() 
    {
             super.onStop();
            Toast.makeText(Activity1.this, “onStop-Activity1″, Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onDestroy() 
    {
            super.onDestroy();
           Toast.makeText(Activity1.this, “onDestroy-Activity1″, Toast.LENGTH_SHORT).show();
    }
}

 Layout for Activity1.java:- 

activity1.xml:-

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:orientation=”vertical”
    android:gravity=”center” >

    <Button android:id=”@+id/btnCAllActivity2″
        android:gravity=”center”
        android:layout_gravity=”center”
        android:layout_width=”150dip”
        android:layout_height=”wrap_content”
        android:text=”Activity2 >” />

</LinearLayout>

 

We have create a button named  ”btnCAllActivity2”  in activity1.xml which is referred in Activity1.java So that user can call Avtivity2 and can find which Override Functions of Activity1 and Activity2 are been called when Activity2 is called and Activity1 goes in stack.

Moreover for tracking which Override Function is Currently Running , We have put the “Toasts” in each and

every Override Functions in both the activities.:-

In Activity1 we have put :-

Toast.makeText(Activity1.this, “onCreate-Activity1″, Toast.LENGTH_SHORT).show();

inside the onCreate() Method. Like wise in other override Functions:-

 @Override
    protected void onStart() 
    {
        super.onStart();
        Toast.makeText(Activity1.this, “onStart-Activity1″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume()
    {    
        super.onResume();
       Toast.makeText(Activity1.this, “onResume-Activity1″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() 
    {
         super.onPause();
        Toast.makeText(Activity1.this, “onPause-Activity1″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() 
    {
           super.onStop();
         Toast.makeText(Activity1.this, “onStop-Activity1″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() 
    {
          super.onDestroy();
         Toast.makeText(Activity1.this, “onDestroy-Activity1″, Toast.LENGTH_SHORT).show();
    }

 

Now we will create Activity2:- 

package  com.lifecycle.example;

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

public class Activity2 extends Activity 
{

      Button btnCAllActivity1;
@Override
public void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity2);
      Toast.makeText(Activity2.this, “onCreate-Activity2″, Toast.LENGTH_SHORT).show();

        btnCAllActivity1 = (Button) findViewById(R.id.btnCAllActivity1); 
         btnCAllActivity1.setOnClickListener(new OnClickListener()
          {
                            @Override
                            public void onClick(View v) 
                            {
                                     finish();
                          }
        });

}

@Override
    protected void onStart() 
    {
         super.onStart();
         Toast.makeText(Activity2.this, “onStart-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume()
    {
           super.onResume();
         Toast.makeText(Activity2.this, “onResume-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() 
    {
            super.onPause();
           Toast.makeText(Activity2.this, “onPause-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() 
    {
         super.onStop();
        Toast.makeText(Activity2.this, “onStop-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() 
    {
            super.onDestroy();
            Toast.makeText(Activity2.this, “onDestroy-Activity2″, Toast.LENGTH_SHORT).show();
    }

}

 Layout for Activity2.java:-

activity2.xml:-

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:orientation=”vertical”
    android:gravity=”center” >

    <Button android:id=”@+id/btnCAllActivity1″
        android:gravity=”center”
        android:layout_gravity=”center”
        android:layout_width=”150dip”
        android:layout_height=”wrap_content”
        android:text=”Activity1″ />

</LinearLayout>

 In Activity2 we have put :-

Toast.makeText(Activity2.this, “onCreate-Activity2″, Toast.LENGTH_SHORT).show();

 inside the onCreate() Method. Like wise in other override Functions:-

@Override
    protected void onStart() 
    {
        super.onStart();
       Toast.makeText(Activity2.this, “onStart-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onResume()
    {
       super.onResume();
       Toast.makeText(Activity2.this, “onResume-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPause() 
    {
         super.onPause();
        Toast.makeText(Activity2.this, “onPause-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStop() 
    {
       super.onStop();
      Toast.makeText(Activity2.this, “onStop-Activity2″, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() 
    {
         super.onDestroy();
        Toast.makeText(Activity2.this, “onDestroy-Activity2″, Toast.LENGTH_SHORT).show();
    }

 

AndroidManifest.xml For the App:-

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
    package=”com.lifecycle.example”
    android:versionCode=”1″
    android:versionName=”1.0″ >

    <uses-sdk android:minSdkVersion=”8″ />

    <application android:icon=”@drawable/ic_launcher” android:label=”@string/app_name” >

        <activity android:name=”.Activity1″ android:label=”@string/app_name”  android:configChanges=”keyboardHidden|orientation”>
            <intent-filter>
                <action android:name=”android.intent.action.MAIN” />
                <category android:name=”android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>

          <activity android:name=”.Activity2″ android:configChanges=”keyboardHidden|orientation” >

        </activity>

    </application>

</manifest>

 

We have put android:configChanges=”keyboardHidden|orientation” for both the Activity Declaration so that the Override Functions will not be get called on Screen Keyboard Showing and at Screen Orientation change.

In Activity2 we will Finish the Activity2 instead of calling Activity1 when we press the button “btnCAllActivity1” Labeled “Activity1”  because we want to show what happens (i.e which override Functions are called when an Activity Get Finished) when an Activity Get Finished , Moreover the Activity1 is already in the stack which is going to be come to front automatically when Activity2 is Destroyed.

Following are the Sequential Screen-Shots of the above Application Showing the Lifecycle Flow of both the Activities:-

When the App runs:-

Screen1

Activity1 Created:

screen2

Activity1 onStart() Method Called:

screen3

Activity1 onResume() method called:

screen4

Activity1 in Live State(Running):

Now press Button “btnCAllActivity2″ Labeled “Activity2”

This will called Activity2 which is displayed Following:-

screen5

Toast Showing OnPause() Method of Activity1 Called:

screen6

Activity2 onCreate() Method Called:

screen7

Activity2 onStart() Method Called:

screen8

Activity2 onResume() Method Called:

Screen9

Now after completion of Activity2 Methods upto onResume() the onStop() Method of Activity1 will be Called:

screen10

Activity2 in Running(Live) Mode:

Now press Button “btnCAllActivity1″ Labeled “Activity1” Of Activity2, This will Finishes Activity2 As per our programming.

This will called Activity1 which is displayed Following:-

Screen11

With this onPause() Method of Activity2 will be called:

Screen12

As Activity1 is created at the time of app Start , Now the onStart Method() of Activity1 Will be Called Again:

Screen13

Activity1 onResume() method called:

Screen14

now onStop() Method of Activity2 will be called:

screen15

Activity2 onDestroy() method called:

screen16

Activity1 in Running/Live State:

Now Press Back button of the Android Device, we will get the following screen:-

screen17

Activity1 will disappear from the screen and onPause() method of Activity1 will be called:

screen19

Activity1 onStop() method will be called:

screen20

At last onDestroy() Method of Activity1 will be called:

That the Android Activity LifeCycle Demonstration:

Thats All for this Tutorial: Happy Programming :-)

Skip to toolbar