博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Activity生命周期的运行流程
阅读量:6702 次
发布时间:2019-06-25

本文共 5451 字,大约阅读时间需要 18 分钟。

Activity的生命周期运行流程:
 ·当Activity第1次被启动:
    ·onCreate()->onStart()->onResume()
 ·当Activity被遮挡后再次显示:

   ·onRestart->onStart()->onResume()

 ·当Activity被遮挡:

    ·onPause()->onStop()
 ·当点击返回(Back)键:
   ·onPause()->onStop()->onDestroy()
详细请參看測试案例:
创建三个Activity
MainActivity
SecondActivity
SmallActivity(小号的Activity。在激活的时候仅仅会遮挡上一个Activity的一部分。即:上一个Activity不会全然被
smallActivity遮挡,所以这样的情况上一个Activity不会onStop())
SmallActivity创建的时候要做特殊调整
在配置文件里调整ctivity的主题为:
 android:theme="@android:style/Theme.Translucent.NoTitleBar" 
在布局文件里调整
把根节点调整大小为以下的大小,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="#207044"
    tools:context=".SmallActivity" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SmallActivity"
        android:textSize="30sp" />
</RelativeLayout>

MainActivity:

package com.example.activity_test;import android.location.GpsStatus.Listener;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends Activity {	private final String TAG = "Activity LifeCycle";	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		Log.d(TAG, "MainActivity -> onCreate()");		InnerOnClickListener listener = new InnerOnClickListener();		findViewById(R.id.btn_SecondActivity).setOnClickListener(listener);		findViewById(R.id.btn_Small).setOnClickListener(listener);	}	private class InnerOnClickListener implements OnClickListener{		@Override		public void onClick(View v) {			// TODO Auto-generated method stub			switch (v.getId()) {			case R.id.btn_SecondActivity:				startActivity(new Intent(getApplicationContext(),SecondActivity.class));				break;			case R.id.btn_Small:				startActivity(new Intent(getApplicationContext(),SmallActivity.class));				break;			default:				break;			}		}	}	@Override	protected void onStart() {		// TODO Auto-generated method stub		super.onStart();		Log.d(TAG, "MainActivity -> onStart()");	}	@Override	protected void onResume() {		// TODO Auto-generated method stub		super.onResume();		Log.d(TAG, "MainActivity -> onResume()");	}	@Override	protected void onRestart() {		// TODO Auto-generated method stub		super.onRestart();		Log.d(TAG, "MainActivity -> onRestart()");	}	@Override	protected void onPause() {		// TODO Auto-generated method stub		super.onPause();		Log.d(TAG, "MainActivity -> onPause()");	}	@Override	protected void onStop() {		// TODO Auto-generated method stub		super.onStop();		Log.d(TAG, "MainActivity -> onStop()");	}	@Override	protected void onDestroy() {		// TODO Auto-generated method stub		super.onDestroy();		Log.d(TAG, "MainActivity -> onDestroy()");	}}
SecondActivity:

package com.example.activity_test;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class SecondActivity extends Activity {	private final String TAG = "Activity LifeCycle";	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_second);		Log.e(TAG, "SecondActivity -> onCreate()");	}	@Override	protected void onStart() {		// TODO Auto-generated method stub		super.onStart();		Log.e(TAG, "SecondActivity -> onStart()");	}	@Override	protected void onResume() {		// TODO Auto-generated method stub		super.onResume();		Log.e(TAG, "SecondActivity -> onResume()");	}	@Override	protected void onRestart() {		// TODO Auto-generated method stub		super.onRestart();		Log.e(TAG, "SecondActivity -> onRestart()");	}	@Override	protected void onPause() {		// TODO Auto-generated method stub		super.onPause();		Log.e(TAG, "SecondActivity -> onPause()");	}	@Override	protected void onStop() {		// TODO Auto-generated method stub		super.onStop();		Log.e(TAG, "SecondActivity -> onStop()");	}	@Override	protected void onDestroy() {		// TODO Auto-generated method stub		super.onDestroy();		Log.e(TAG, "SecondActivity -> onDestroy()");	}}
SmallActivity:

package com.example.activity_test;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Menu;public class SmallActivity extends Activity {	private final String TAG = "Activity LifeCycle";	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_small);		Log.i(TAG, "SmallActivity -> onCreate()");	}	@Override	protected void onStart() {		// TODO Auto-generated method stub		super.onStart();		Log.i(TAG, "SmallActivity -> onStart()");	}	@Override	protected void onResume() {		// TODO Auto-generated method stub		super.onResume();		Log.i(TAG, "SmallActivity -> onResume()");	}	@Override	protected void onRestart() {		// TODO Auto-generated method stub		super.onRestart();		Log.i(TAG, "SmallActivity -> onRestart() ");	}	@Override	protected void onPause() {		// TODO Auto-generated method stub		super.onPause();		Log.i(TAG, "SmallActivity -> onPause()");	}	@Override	protected void onStop() {		// TODO Auto-generated method stub		super.onStop();		Log.i(TAG, "SmallActivity ->  onStop()");	}	@Override	protected void onDestroy() {		// TODO Auto-generated method stub		super.onDestroy();		Log.i(TAG, "SmallActivity -> onDestroy()");	}}

转载地址:http://nogoo.baihongyu.com/

你可能感兴趣的文章
Linux命令替换字符串
查看>>
两个文件比较之comm命令
查看>>
搭建Telnet服务器
查看>>
Java基础-Java中的内存分配与回收机制
查看>>
快速部署ldap服务
查看>>
你好,C++(34)有一只叫做多利的羊 6.2.4 拷贝构造函数
查看>>
[Android Studio] Android Studio如何提示函数用法
查看>>
[Web 前端] 解决因inline-block元素导致的空白间距和元素下沉
查看>>
ORACLE数据库 常用命令和Sql常用语句
查看>>
《结对-贪吃蛇-需求分析》
查看>>
面向对象编程
查看>>
java 常用工具类的使用<一>
查看>>
A Neural Algorithm of Artistic Style
查看>>
Camera Calibration 相机标定:原理简介(三)
查看>>
ArrayList的使用方法【转载】
查看>>
String.Format格式说明
查看>>
Linux的proc文件系统详解
查看>>
Beta 冲刺(6/7)
查看>>
Kubernetes集群(概念篇)
查看>>
微软Hololens学院教程- Holograms 101: Introduction with Device【微软教程已经更新,本文是老版本】...
查看>>