Monday, December 8, 2014

107-ADT18 Managing App Life Cycle


---
107-ADT18 Managing App Life Cycle

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”.
---

No comments:

Post a Comment