您的位置:首页 > 移动开发 > Android开发

Android :LaunchMode 生命周期

2015-11-03 16:51 323 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: