What is Activity in Android?

What is Activity in Android?

 Activity is a class that is use for interaction with screen and user,such as dial the number,call phone,send Email, capture photos, play video etc., so the Activity class is responsible for place the controls on windows.

In application there are multiple activities, that are bound to each other and one of them is main Activity.When a new activity start then previous activity stopped, the system preserve the activity in a stack.


NOTE - 

  •    Super class of Activity
             ContextThemeWrapper
  •  The Direct subclass of activity.

                AccountAuthenticatorActivity, ActivityGroup,ExpendableListActivity,FragmentActivity,                    
                ListActivity,NativeActivity,TestActivity

  • Indirect Subclasses of Activity
          ActionBarActivity,TabActivity,Launcheractivity,PrefrenceActivity


Life cycle of Activity
Activity in the system managed as an activity stack.When a new activity started, it will be on the top of the stack and become running state and previous activity always below on the stack.
There are four state of Activity.
1. The running and active activity always will be  in the foreground of the screen.
2.If the activity has lost focused,(like when dialog ,popup or other thing open on the activity)It is in paused                               state.
3.When a new activity open on previous,then previous goes in stopped state.
4. If an activity is paused and stopped, the system can drop the activity from the memory. or killing its process.


                                                    Activity lifecycle

Callbacks Method of Activity

onCreate() - This is the callback and called when the activity first created.

onStart()  - It is called when activity is becoming visible to user.

onResume() - It is called when activity  interacting to the user.

onPaused()  - called when system is resuming the other activity then current activity become paused;

onStop()  - called when activity is no longer visible to user.

onDestroy()  - called when system finished the instance of the activity.

For more details  click here

Create an application in Android Studio

step 1. Define the name of application


step 2. check  which type of application you want to develop.(check in phone and Tablet)



Step 3. select which type of activity  you want.


Step 4. Define the name of Activity




In this example, i am focus on callback methods of Activity.
A Toast is using to show the method state when they called. Toast is a widget that is use to show as popup message for small interval of time.

Here is ActivityDemo.java

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this,"onCreate() method call",Toast.LENGTH_LONG).show();
    }
/* onStart() method*/
    public  void onStart(){
        super.onStart();
        Toast.makeText(this,"onStart() method call",Toast.LENGTH_LONG).show();
    }
/*onResume method*/
    public  void onResume(){
        super.onResume();
        Toast.makeText(this,"onResume() method call",Toast.LENGTH_LONG).show();
    }
    /*onPause() method*/
    public  void onPause(){
        super.onPause();
        Toast.makeText(this,"onPause() method call",Toast.LENGTH_LONG).show();
    }
    /*onstop method*/
    public  void onStop(){
        super.onStop();
        Toast.makeText(this,"onStop() method call",Toast.LENGTH_LONG).show();
    }
    /*onDestroy method*/
    public  void onDestroy(){
        super.onDestroy();
        Toast.makeText(this,"onDestroy() method call",Toast.LENGTH_LONG).show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Here is main_activity.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:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>



AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="demo.androidheight.com.myactivitydemo" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>


OUTPUT








Download complete source code: link

Feel free to ask your doubt in comment.




SHARE

About prabhakar jha

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment