Activity的生命周期测试

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.运行效果图。 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Android Activity和Fragment的生命周期测试代码: MainActivity.java ``` import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @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 onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy()"); } } ``` fragment_test.xml ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Test"/> </LinearLayout> ``` TestFragment.java ``` import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.fragment.app.Fragment; public class TestFragment extends Fragment { private static final String TAG = "TestFragment"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_test, container, false); Log.i(TAG, "onCreateView()"); return view; } @Override public void onStart() { super.onStart(); Log.i(TAG, "onStart()"); } @Override public void onResume() { super.onResume(); Log.i(TAG, "onResume()"); } @Override public void onPause() { super.onPause(); Log.i(TAG, "onPause()"); } @Override public void onStop() { super.onStop(); Log.i(TAG, "onStop()"); } @Override public void onDestroyView() { super.onDestroyView(); Log.i(TAG, "onDestroyView()"); } @Override public void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy()"); } } ``` MainActivity.java中添加以下代码: ``` getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new TestFragment()).commit(); ``` 在Logcat中可以看到以下输出: ``` onCreate() onStart() onResume() onCreateView() onStart() onResume() ``` 当你按下返回键时,可以看到以下输出: ``` onPause() onStop() onDestroyView() onDestroy() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值