Android Activity Life Cycle
An
Activity is usually a single screen that the users sees on
the device at a one time. Any application that has been developed by
anyone it typically contains multiple activities.
* Most
management of the life cycle is done automatically by the system via the
activity Stack
*
Activity manager is responsible for creating , destroying and managing
activities.
For Example when
the user starts an application for the first time, the activity manager will
create
its
activity and put it into the screen. later, when the user navigating to
different screens . The activity manager will move to the previous activity to
a holding place .This way if the user wants to go back to older activity , it
can be started more quickly , older activities that the user has not used
in a while will be destroyed in order to free more space
fort the currently one .
In Activity Life Cycle we are having following methods:
1. onCreate()
2.
onStart()
3.
onRestart()
4.
onResume()
5.
onPause()
6.
onStop()
7.
onDestroy()
1. onCreate(); Called when the activity is first
created. this is where you should do all of your normal static setup.
create views bind data to lists etc, this method also provides you with a
bundle containing the activities previous state.
2. onStart(): Called when the activity become visible to the
user.
3. onRestart(); called after your activity has been
stopped. prior to it being started again
4. onResume(); Called when the activity starts interacting
with the user. At this point your activity is at the top of the stack.
5. 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 graphics that may be
consuming CPU .
6. onStop(): Called when the activity is no longer visible
to the user. and another activity has been resumed and is covering this one.
7. onDestroy(): Called when your finishing activity.
The activity first time loads the methods are called as bilow:
onCreate()
onStart()
onResume()
When try to click back button or finish the activity the following
methods are called:
onPause()
onStop()
onDestroy()
When you click on home button the activity goes to background and
below methods are called:
onPause()
onStop()
After hitting home button again if your opening the previous
activity which is in background the following methods are called:
onRestart()
onStart()
onResume();
activity_main.xml
----------------------------
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Activity Life Cycle" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
MainActivity.Java
-----------------------
package com.android.anil.activitylifecycle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast.makeText(getApplicationContext(),"OnCreated() Method",Toast.LENGTH_LONG).show(); } @Override protected void onStart() { super.onStart(); Toast.makeText(getApplicationContext(),"OnStart() Method",Toast.LENGTH_LONG).show(); } @Override protected void onResume() { Toast.makeText(getApplicationContext(),"OnResume() Method",Toast.LENGTH_LONG).show(); super.onResume(); } @Override protected void onRestart() { super.onRestart(); Toast.makeText(getApplicationContext(),"OnRestart() Method",Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); Toast.makeText(getApplicationContext(),"OnDestory() Method",Toast.LENGTH_LONG).show(); } @Override protected void onPause() { super.onPause(); Toast.makeText(getApplicationContext(),"OnPause() Method",Toast.LENGTH_LONG).show(); } @Override protected void onStop() { super.onStop(); Toast.makeText(getApplicationContext(),"OnStop() Method",Toast.LENGTH_LONG).show(); } }
Output is:
-------------------
No comments:
Post a Comment