Android :LaunchMode 生命周期



LaunchMode:

1、standard:在栈顶直接新建读取;

2、singTop:如果栈顶是接下来要读取的,复用栈顶;如果不是就新建;

3、singleTask:删除其以上所有,以其为栈顶读取;

4、singleInstance:调用读取的为栈顶;


<activity
            android:name=".ActivityB"
            android:LaunchMode="1,2,3,4"
            android:label="@string/app_name" >
         </activity>





生命周期:

<span style="font-size:18px;">package com.hx.zy.sm;

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


public class MainActivity extends Activity {
	
	private  final String TGA="MainActivity 生命周期进入";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.d(TGA, "onCreate");
		
		final Context context=this;
		Button button2=(Button) findViewById(R.id.button2);
		
		Button button1=(Button) findViewById(R.id.button1);
		
		button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context,ActivityB.class);
				startActivity(intent);
			}
		});
		button2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				MainActivity.this.finish();
				
			}
		});
	}
	
	
	@Override
	protected void onStart() {
		
		super.onStart();
		Log.d(TGA, "onStart");
	}

	@Override
	protected void onRestart() {
		
		super.onRestart();
		Log.d(TGA, "onRestart");
	}


	@Override
	protected void onResume() {
		
		super.onResume();
		Log.d(TGA, "onResume");
	}

	@Override
	protected void onPause() {
	
		super.onPause();
		Log.d(TGA, "onPause");
	}

	@Override
	protected void onStop() {
	
		super.onStop();
		Log.d(TGA, "onStop");
	}


	@Override
	protected void onDestroy() {
	
		super.onDestroy();
		Log.d(TGA, "onDestroy");
	}

}</span>


<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"
    
    tools:context="com.hx.zy.sm.MainActivity" >

    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动" />

    <Button
        android:id="@+id/button2"
        android:layout_below="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="完成" />
</RelativeLayout>




<span style="font-size:18px;">package com.hx.zy.sm;

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

public class ActivityB extends Activity{
	
	private  final String TGA="<span style="font-family: Arial, Helvetica, sans-serif;">ActivityB </span><span style="font-family: Arial, Helvetica, sans-serif;">生命周期“;</span>
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_b);
		Log.d(TGA, "onCreate");
		
		Button button3 = (Button)findViewById(R.id.button3);
		button3.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//Intent intent = new Intent(ActivityB.this,MainActivity.class);
				
				//startActivity(intent);
				
				ActivityB.this.finish();		
			}	
		});
	}
	@Override
	protected void onStart() {
		
		super.onStart();
		Log.d(TGA, "onStart");
	}

	@Override
	protected void onRestart() {
		
		super.onRestart();
		Log.d(TGA, "onRestart");
	}


	@Override
	protected void onResume() {
		
		super.onResume();
		Log.d(TGA, "onResume");
	}

	@Override
	protected void onPause() {
	
		super.onPause();
		Log.d(TGA, "onPause");
	}

	@Override
	protected void onStop() {
	
		super.onStop();
		Log.d(TGA, "onStop");
	}


	@Override
	protected void onDestroy() {
	
		super.onDestroy();
		Log.d(TGA, "onDestroy");
	}

}</span>

<span style="font-size:18px;"><?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:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:textSize="25dip"
        android:text="恭喜你,成功了!" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动Main" />

</LinearLayout></span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值