Published using Google Docs
107-ADT18 Managing App Life Cycle
Updated automatically every 5 minutes

107-ADT18 Managing App Life Cycle

This tutorial is based on http://developer.android.com/training/basics/activity-lifecycle/index.html .

1) Create a new Android Project

Give a name Life Cycle Test.

2) Edit MainActivity.java

Replace the initial codes with the following codes…

package com.example.lifecycletest;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.widget.Toast;

public class MainActivity extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tToast("onCreate");

    }

    public void onStart() {

    super.onStart();

    tToast("onStart");

    }

    public void onRestart() {

    super.onRestart();

    tToast("onRestart");

    }

    public void onResume() {

    super.onResume();

    tToast("onResume");

    }

    public void onPause() {

    super.onPause();

    tToast("onPause: bye bye!");

    }

    public void onStop() {

    super.onStop();

    tToast("onStop.");

    }

    public void onDestroy() {

    super.onStop();

    tToast("onDestroy.");

    }

    private void tToast(String s) {

        Context context = getApplicationContext();

        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, s, duration);

        toast.show();

    }

}

3) Run and observe your apps behavior

4) Additional Exercise

Add codes to output log message labeled with “lifecycle”.

(refer tutorial http://android-steps.blogspot.com/2014/12/105-adt-18-build-android-simple-user.html )