码迷,mamicode.com
首页 > 其他好文 > 详细

Activity启动模式

时间:2015-12-28 10:31:44      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

一、标准模式(standrard)

1.当新建一个Activity时,默认情况下就是标准模式,也可以通过AndroidManifest文件显示指定其launchMode为standard

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.activitystartself" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:launchMode="standard"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
2.通过getTaskId()获取任务id号,通过toString()获取当前Activity的属性
public class MainActivity extends Activity {
private TextView tv;

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

tv=(TextView)findViewById(R.id.tv);
tv.setText(String.format("TaskID:%d \nActivatyName:%s",getTaskId(),toString()));

findViewById(R.id.btnStartMainActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
}
}
3.标准模式下所有的Activity都位于同一个任务栈中,先进新出,启动的第一个Activity位于栈底,之后一次叠加。
二、SingleTop模式
SingleTop模式下,处于任务栈栈顶的Activity只能打开一个,其余的可以打开多个。
2.1 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.activitystartself" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BActivity"
android:label="@string/title_activity_b"
android:launchMode="singleTop">
</activity>
</application>

</manifest>
2.2 activity_main.xml
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:id="@+id/tv"
android:layout_height="wrap_content" />

<Button
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动MainActivity"
android:id="@+id/btnStartMainActivity"
android:layout_gravity="center_horizontal" />

<Button
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动BActivity"
android:id="@+id/btnStartBActivity"
android:layout_gravity="center_horizontal" />
</LinearLayout>
2.3 MainActivity.java
package com.example.shiyanshi.activitystartself;

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


public class MainActivity extends Activity {
private TextView tv;

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

tv=(TextView)findViewById(R.id.tv);
tv.setText(String.format("TaskID:%d \nActivatyName:%s",getTaskId(),toString()));

findViewById(R.id.btnStartMainActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
findViewById(R.id.btnStartBActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,BActivity.class));
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
2.4 activity_b.xml
<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.shiyanshi.activitystartself.BActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:id="@+id/tv"
android:layout_height="wrap_content" />

<Button
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动MainActivity"
android:id="@+id/btnStartMainActivity"
android:layout_gravity="center_horizontal" />

<Button
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动BActivity"
android:id="@+id/btnStartBActivity"
android:layout_gravity="center_horizontal" />

</LinearLayout>
2.5 BActivity.java
package com.example.shiyanshi.activitystartself;

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


public class BActivity extends Activity {
private TextView tv;

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

tv=(TextView)findViewById(R.id.tv);
tv.setText(String.format("TaskID:%d \nActivatyName:%s", getTaskId(), toString()));
findViewById(R.id.btnStartMainActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(BActivity.this, MainActivity.class));
}
});
findViewById(R.id.btnStartBActivity).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(BActivity.this, BActivity.class));
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_b, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
三、singleTask模式
singleTask模式下,当打开两个不同的Activity时,此时再打开一个和栈底Activity同类型的Activity时,会把栈顶的Activity弹出,任务栈中只有了一个Activity,再按一次返回就可以直接退出应用程序。
直接修改上面AndroidManifest.xml文件,修改launchMode为singleTask便可
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.activitystartself" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BActivity"
android:label="@string/title_activity_b"
android:launchMode="singleTask"
>
</activity>
</application>

</manifest>

 


四、singleInstance模式
两个不同Activity的任务ID不相同,每一个任务栈中只有一个Activity实例,两个不同的Activity之间可以相互切换,点击一次返回会返回到上一个Activity,再点击一次返回才会退出应用程序。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shiyanshi.activitystartself" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BActivity"
android:label="@string/title_activity_b"
android:launchMode="singleInstance"
>
</activity>
</application>

</manifest>




Activity启动模式

标签:

原文地址:http://www.cnblogs.com/ql698214/p/5081662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!