Android学习笔记--四大组件之Activity

学习android已经有一段时间了。最近发现学习的笔记很混乱,所以决定将笔记陆续的放到博客上。

好了,废话不多说,我们来进入学习正题。

提到Activity,那就必须得找来一张图


这张图相信学过Activity的同学们都会烂熟于胸了吧。不了解activity的同学我们一会儿在下面看代码,看完代码,就会觉得闭着眼也能画出来这图了。

对于这7个生命周期,突然想起一句歌词,七个周期七朵花。。。

onCreate:所有Acitvity启动,都会调用这个方法,所以,我们一般也把代码写到这里面来,不过这个代码相对于一个activity生命周期,他只调用一次。

然后程序调用onStart和onResume,activity开始工作。

当有另外的activity不完全遮盖当前activity,或者用户进行锁屏,系统会调用当前activity的onPause方法,程序暂停。

(请注意,在模拟器下锁屏,系统会调用onStop方法,并且,dialog对activity的遮盖是不会调用onPause方法的,因为dialog并不是一个activity)

当有另外的activity完全遮盖当前activity,或者程序在后台运行,系统会调用当前activity的onStop方法,程序暂停。

当activity重新回到显示界面,系统会依次调用onRestart->onStart->onResume。activity重新运行。

当我们需要关掉一个activity的时候,系统会依次执行onPause->onStop->onDestroy方法。<我们来用个程序说明一下 Main.java

package com.example.test_activitiy_onresure;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity {
	// 初始化系统变量
	private final String TAG = "Activity";

	private Button btn1, btn2, btn3;

	private Dialog dialog;

	// Activity被创建时调用
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i(TAG, "This is onCreate");

		btn1 = (Button) this.findViewById(R.id.button1);
		btn2 = (Button) this.findViewById(R.id.button2);
		btn3 = (Button) this.findViewById(R.id.button3);

		dialog = new Dialog(Main.this);
		dialog.setTitle("Just Dialog");
		dialog.setCanceledOnTouchOutside(true);

		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.show();
			}
		});

		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Main.this, UnCovered.class);
				startActivity(intent);
			}
		});

		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Main.this, Covered.class);
				startActivity(intent);
			}
		});
	}

	// 从后台回到前台时调用
	@Override
	protected void onStart() {
		super.onStart();
		Log.i(TAG, "This is onStart");
	}

	// 在onStop与onStrat之间调用,首次创建activity时不调用此方法。
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.i(TAG, "This is onRestart");
	}

	// 开启锁屏或者没有activity遮盖时调用
	@Override
	protected void onResume() {
		super.onResume();
		Log.i(TAG, "This is onResume");
	}

	// 有程序不完全挡住或锁屏时调用
	@Override
	protected void onPause() {
		super.onPause();
		Log.i(TAG, "This is onPause");
	}

	// activity被挡住或者在后台时调用。
	@Override
	protected void onStop() {
		super.onStop();
		Log.i(TAG, "This is onStop");
	}

	// activity关闭的时候调用
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.i(TAG, "This is onDestroy");
	}
}

XML程序 activity_main

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world"
        tools:context=".Main" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Dialog" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:text="不完全覆盖的Activity" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button2"
        android:text="完全覆盖的Activity" />

</RelativeLayout>

另外还要创建两个空activity,用来覆盖当前activity。一个UnCovered,一个Covered。

当第一次打开程序的时候


当有dialog挡住activity的时候

没有任何的log提示,说明dialog挡住的activity不调用任何生命周期。

当有dialog模式的activity挡住activity的时候


当activity被完全挡住的时候


中间那行是启动一个新的activity提示的log

当关闭activity的时候



7个周期介绍完了,还有3个常用的方法,这3个方法在一般的android书中基本没有介绍,

onWindowFocusChanged、onSaveInstanceState、onRestoreInstanceState。

关于这三个方法,大家可以看看liuhe688的原创。我在这里就不转载啦,因为自己掌握的也不是很清楚,所以留个链接以后来学习。

liuhe688的原创

http://blog.csdn.net/liuhe688/article/details/6733407

第一篇博客,写的不好,希望能对自己和大家有帮助。欢迎关注我的微博,最近刚开始玩,求互粉。http://weibo.com/u/2873751722

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值