码迷,mamicode.com
首页 > 移动开发 > 详细

android Activity生命周期的例子

时间:2015-12-02 12:28:20      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.yanlei.yl2;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "生命周期:";
    String Str = "";
    TextView pTextView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //当创建此Activity的时候回调
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate");
        try {
            Str = Str + "创建:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }

    }

    @Override
    protected void onDestroy() {
        //当销毁此Activity的时候回调
        super.onDestroy();
        Log.e(TAG, "onDestroy");


        try {
            Str = Str + "摧毁:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

    @Override
    protected void onPause() {
        //当暂停此Activity的时候回调
        super.onPause();
        Log.e(TAG, "onPause");
        try {
            Str = Str + "暂停:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            // Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

    @Override
    protected void onRestart() {
        //当重新开始此Activity的时候回调
        super.onRestart();
        Log.e(TAG, "onRestart");
        try

        {
            Str = Str + "重新启动:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

    @Override
    protected void onResume() {
        //当显示展示此Activity的界面的时候回调
        super.onResume();
        Log.e(TAG, "onResume");
        try {
            Str = Str + "继续:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

    @Override
    protected void onStart() {
        //当使用此Activity可以接受用户操作的时候回调
        super.onStart();
        Log.e(TAG, "onStart");
        try {
            Str = Str + "开始:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

    @Override
    protected void onStop() {
        //当停止此Activity的时候回调
        super.onStop();
        Log.e(TAG, "onStop");
        try {
            Str = Str + "停止:\n";
            pTextView.setText(Str);
        } catch (Exception e) {
            //Log.e(TAG, "error : "+e.getMessage(), e);
        }
    }

}

日志如下:

onCreate
onStart
onResume

onPause
onStop
onDestroy

android Activity生命周期的例子

标签:

原文地址:http://www.cnblogs.com/gisoracle/p/5012426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!