Sunday 16 April 2017

Various Activity Life Phases of Android Application

FileName: 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" android:background="#CF3A24"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Mycurlycode" android:id="@+id/textView" android:textColor="#FCC9B9" android:textSize="30sp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>

FileName: MainActivity.java

package com.example.abbu.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = "mycurlycodeMessage";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"onCreate");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");
    }


    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState");
    }



    @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);
    }
}

FileName: strings.xml

<resources>
    <string name="app_name">My Application</string>
    <string name="action_settings">Settings</string>
</resources>

Output:

Various Activity Life Phases of Android Application
Various Activity Life Phases of Android Application
As soon as we click on Android Application Icon, the Launcher or Main Activity starts and it calls:
onCreate, onStart and OnResume methods on start of Android Application
onCreate, onStart and OnResume methods on start of Android Application
Now, when we click on home Button, it calls:
onPause, onSaveInstanceState and onStop methods are called on clicking home button
onPause, onSaveInstanceState and onStop methods on clicking home button
When we again click on home button and restart that Android Application, it calls:
onRestart, onStart and onResume methods are called on again clicking and selecting that particular app
onRestart, onStart and onResume methods on again clicking on home button and selecting that particular application
For Landscape Orientation, it calls:
Methods called on Landscape Orientation.
Methods called on Landscape Orientation.
For Portrait Orientation, it calls:
Methods called in Portrait Orientation.
Methods called in Portrait Orientation.
Finally, when we click on back button, it calls:
onDestroy on clicking back button.
onDestroy on clicking back button.

No comments:

Post a Comment