Activity生命周期详解

原理图:



文字描述如下:

Activity的作用:
起显示作用,他是用来和用户交互的。也是一个view的容器


1 完整的生命周期:
 onCreate() --> onStart() --> onResume()   activiyt已经正常显示
 点击回退键
 onPause() --> onStop() --> onDetroy()


2 可视的生命周期
 onCreate() --> onStart() --> onResume()   activiyt已经正常显示
 打开一个activity。该activity完全覆盖上一个activity
 onPause() ---> onStop()
 点击回退键
 onRestart()  -->  onStart() ---> onResume()
 点击回退键
onPause() --> onStop() --> onDetroy()


3 android:theme="@android:style/Theme.Dialog"
  就可以把activity变成对话框的效果
 onCreate() --> onStart() --> onResume()   activiyt已经正常显示
 打开一个activity。该activity没有完全覆盖上一个activity
 onPause()
 点击回退键
 onResume()


------------如果要将完全覆盖-----(转换成)------>>部分覆盖,只需要在清单文件AndroidManifest.xml中在

<activity/>标签中加上这么一句:android:theme="@android:style/Theme.Dialog"------------------------------


代码如下:

1)main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="get"
        android:text="打开" />

</LinearLayout>


2)main2.xml

android:layout_gravity="center"      //控件居中

android:gravity="center"   //控件中的文本居中


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center" 
        android:gravity="center" 
        android:text="第二个activity"
        android:textColor="#00f"/>

</LinearLayout>


3)MainActivity

package com.njupt.activitylife;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		System.out.println("onCreate");
	}

	@Override
	protected void onStart() {
		super.onStart();
		
		System.out.println("onStart()");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	    
		System.out.println("onResume()");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	    
		System.out.println("onPause()");
	}
	
	
	@Override
	protected void onStop() {
		super.onStop();
	    
		System.out.println("onStop()");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	    
		System.out.println("onDestroy()");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		
		System.out.println("onRestart()");
	}
	public void get(View v){
		
		Intent intent = new Intent(this,Main2Activity.class);
		
		startActivity(intent);
	}
	
	
	@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;
	}

}

4)Main2Activity

package com.njupt.activitylife;

import android.app.Activity;
import android.os.Bundle;

public class Main2Activity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main2);
	}
}

5)AndroidManifest.xml

最后在清单文件中将Main2Activity注册上去

<activity android:name="com.njupt.activitylife.Main2Activity" android:label="第二个Activity"
            android:theme="@android:style/Theme.Dialog"/>



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值