Activity的生命周期测试

本文通过一个简单的Android应用示例,展示了如何观察Activity的生命周期方法,包括onCreate、onStart、onResume、onPause、onStop、onDestroy等,并介绍了如何通过LogCat查看这些方法的调用时机。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 训练目标

1) 观察Activity生命周期经历的主要方法

2) 如果使用LogCat观察输出,掌握如何设置LogFilter

3) 观察何时调用onPause方法?

4) 观察何时调用onStop方法?

2.核心代码。

<pre name="code" class="java">[java] view plain copy
package com.test4.lifecycle;  
  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.Menu;  
import android.view.MenuItem;  
import android.view.View;  
  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Log.i("MainActivity","onCreat");  
    }  
      
    public void toNormal(View view){  
        Intent intentNormal = new Intent (this, NormalActivity.class);  
        startActivity(intentNormal);  
    }  
      
    public void toDialog(View view){  
        Intent intentDialog = new Intent (this, DialogActivity.class);  
        startActivity(intentDialog);  
    }     
  
    @Override  
    protected void onDestroy() {  
        // TODO Auto-generated method stub  
        super.onDestroy();  
        Log.i("MainActivity","onDestroy");  
    }  
  
    @Override  
    protected void onPause() {  
        // TODO Auto-generated method stub  
        super.onPause();  
        Log.i("MainActivity","onPause");  
    }  
  
    @Override  
    protected void onRestart() {  
        // TODO Auto-generated method stub  
        super.onRestart();  
        Log.i("MainActivity","onRestart");  
    }  
  
    @Override  
    protected void onResume() {  
        // TODO Auto-generated method stub  
        super.onResume();  
        Log.i("MainActivity","onResume");  
    }  
  
    @Override  
    protected void onStart() {  
        // TODO Auto-generated method stub  
        super.onStart();  
        Log.i("MainActivity","onStart");  
    }  
  
    @Override  
    protected void onStop() {  
        // TODO Auto-generated method stub  
        super.onStop();  
    Log.i("MainActivity","onStop");  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.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();  
        if (id == R.id.action_settings) {  
            return true;  
        }  
        return super.onOptionsItemSelected(item);  
    }  
}  

NormalActivity和DialogActivity并没有进行改动。

activity_main:
[html] view plain copy
<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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context="com.test4.lifecycle.MainActivity" >  
  
    <Button  
        android:id="@+id/toNormal"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="136dp"  
        android:onClick="toNormal"  
        android:text="@string/toNormal" />  
  
    <Button  
        android:id="@+id/toDialog"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_below="@+id/toNormal"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="27dp"  
        android:onClick="toDialog"  
        android:text="@string/toDialog" />  
  
</RelativeLayout>  

activity_normal:
[html] view plain copy
<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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context="com.test4.lifecycle.NormalActivity" >  
  
    <TextView  
        android:id="@+id/textView2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/Normal_output"  
        android:textAppearance="?android:attr/textAppearanceMedium" />  
  
</RelativeLayout>  

activity_dialog:
[html] view plain copy
<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:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context="com.test4.lifecycle.DialogActivity" >  
  
    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="@string/Dialog_output"   
        android:textAppearance="?android:attr/textAppearanceMedium" />  
  
</RelativeLayout>  

AndroidManifest.xml(清单文件中的一部分):
[html] view plain copy
<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>  
    <activity  
        android:name=".NormalActivity"  
        android:label="@string/title_activity_normal" >  
    </activity>  
    <activity  
        android:name=".DialogActivity"  
        android:theme="@android:style/Theme.Dialog"  
        android:label="@string/title_activity_dialog" >  
    </activity>  
</application>  



3.运行效果图。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值