Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32026
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 85
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 11:11
个人简介

everything is up to you

文章分类

全部博文(8)

文章存档

2014年(8)

分类: Android平台

2014-05-09 23:35:19

1:standard 标准模式,不需要配置,系统默认的加载模式
启动目标Activity时,Android总会为目标Activity创建一个新的实例,并放到当前Task栈中,这种模式不会启动新的Task,新的Activity将被添加到原有的Task中
例子代码如下:

点击(此处)折叠或打开

  1. package com.example.androidgov;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;

  7. public class StandardActivity extends Activity {

  8.     TextView textview1 = null;
  9.     
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_standard);
  14.         textview1 = (TextView)findViewById(R.id.stand_textview1);
  15.         textview1.setText(this.getTaskId() + "--" + this.toString());
  16.     }

  17.     public void startActivity( View view ) {
  18.         Intent intent = new Intent(StandardActivity.this, StandardActivity.class);
  19.         startActivity(intent);
  20.     }
  21. }
布局文件如下:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:id="@+id/container"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     tools:context="com.example.androidgov.StandardActivity"
  8.     tools:ignore="MergeRootFrame" >

  9.     <Button
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:onClick="startActivity"
  13.         android:text="startMySelf" />

  14.     <TextView
  15.         android:id="@+id/stand_textview1"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="wrap_content" />

  18. </LinearLayout>
运行结果如下图:

点击启动startMyself按钮

再次点击

由此可见,每次都在同一个Task中启动新的Activity

2:singleTop Task顶单例模式
与standard模式差不多,只是如果要被启动的Activity已经位于Task栈顶,那么这个Activity将不会被启动,不会生成Activity实例。而如果要被启动的Activity没有位于栈顶,系统会创建Activity实例,并把它置于Task栈顶
例子代码(AndroidManifest.xml中,activity节点配置一个launchMode属性,如下所示)

点击(此处)折叠或打开

  1. <activity
  2.             android:name="com.example.androidgov.SingleTopActivity"
  3.             android:label="@string/title_activity_single_top"
  4.             android:launchMode="singleTop" >
  5.             <intent-filter>
  6.                 <action android:name="android.intent.action.MAIN" />

  7.                 <category android:name="android.intent.category.LAUNCHER" />
  8.             </intent-filter>
  9.         </activity>
  10.         <activity
  11.             android:name="com.example.androidgov.OtherActivity"
  12.             android:label="@string/title_activity_other" >
  13.         </activity>
SingleTopActivity.java代码

点击(此处)折叠或打开

  1. package com.example.androidgov;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;

  7. public class SingleTopActivity extends Activity {

  8.     TextView textview1 = null;
  9.     
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_single_top);
  14.         textview1 = (TextView)findViewById(R.id.singleTop_textview1);
  15.         textview1.setText(this.getTaskId() + "--" + this.toString());
  16.     }

  17.     
  18.     public void startSecond( View view ) {
  19.         Intent intent = new Intent(SingleTopActivity.this, OtherActivity.class);
  20.         startActivity(intent);
  21.     }
  22.     
  23.     public void startSelf( View view ) {
  24.         Intent intent = new Intent(SingleTopActivity.this, SingleTopActivity.class);
  25.         startActivity(intent);
  26.     }
  27. }
布局文件:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:id="@+id/container"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     tools:context="com.example.androidgov.SingleTopActivity"
  8.     tools:ignore="MergeRootFrame" >

  9.     <Button
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:onClick="startSecond"
  13.         android:text="startOther" />


  14.     <Button
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content"
  17.         android:onClick="startSelf"
  18.         android:text="startMyself" />

  19.     <TextView
  20.         android:id="@+id/singleTop_textview1"
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content" />

  23. </LinearLayout>
OtherActivity.java代码文件

点击(此处)折叠或打开

  1. package com.example.androidgov;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.TextView;

  7. public class OtherActivity extends Activity {

  8.     TextView textview1 = null;
  9.     
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_other);
  14.         textview1 = (TextView)findViewById(R.id.other_textview1);
  15.         textview1.setText(this.getTaskId() + "--" + this.toString());
  16.     }

  17.     public void startActivity( View view ) {
  18.         Intent intent = new Intent(OtherActivity.this, SingleTopActivity.class);
  19.         startActivity(intent);
  20.     }
  21. }
布局文件:

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:id="@+id/container"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     tools:context="com.example.androidgov.StandardActivity"
  8.     tools:ignore="MergeRootFrame" >

  9.     <Button
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content"
  12.         android:onClick="startActivity"
  13.         android:text="startSingleTop" />


  14.     <TextView
  15.         android:id="@+id/other_textview1"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="wrap_content" />

  18. </LinearLayout>
运行结果如图(运行过程为:开启->点击startOther->点击startSingleTop->点击startSelf->点击startOther)


由以上图可知,如果要启动Activity不在栈顶,则会生成实例,置于栈顶位置,如果要启动Activity已经在栈顶,则不会生成实例

3:singleTask Task内单例模式
如果要启动的Activity没有,则系统生成Activity实例,并把它置于Task栈顶
如果要启动的Activity已经存在,但没有位于栈顶,系统把它之上的其他Activity全部移出栈,使得该Activity置于栈顶
如果要启动的Activity已经存在,而且已经位于栈顶,则系统不会生成相应实例
示例代码可以复用SingleTop中的代码进行测试,这里不再演示
4:singleInstance 全局单例模式
如果要启动的Activity不存在,系统会创建一个新的Task,再创建一个Activity实例,将这个实例加入这个新的Task中
如果要启动的Activity已经存在,无论它位于那个应用程序中,系统会把该Activity所在的Task转到前台,从而使该Activity显示出来
另外,这种模式启动的Activity存在的Task中只有这一个Activity,位于栈顶
示例代码可以复用SingleTop中的代码,只是修改AndroidManifest.xml中的配置

点击(此处)折叠或打开

  1. <activity
  2.             android:name="com.example.androidgov.SingleTopActivity"
  3.             android:label="@string/title_activity_single_top" >
  4.             <intent-filter>
  5.                 <action android:name="android.intent.action.MAIN" />

  6.                 <category android:name="android.intent.category.LAUNCHER" />
  7.             </intent-filter>
  8.         </activity>
  9.         <activity
  10.             android:name="com.example.androidgov.OtherActivity"
  11.             android:label="@string/title_activity_other"
  12.             android:launchMode="singleInstance" >
  13.         </activity>
运行如图(运行过程是:启动->点击startSelf->点击startSelf->点击startOther->点击startSingleTop->点击startSelf->点击startOther)

由上图可知,如果以SingleInstance启动Activity,它会生成一个新的示例和一个Task,该实例位于该Task中。如果这个Activity已经存在,启动它的时候只是把它置于前台显示出来,并不会再生成新实例。而且,这个新的Task中只有这一个实例,从这个实例启动的其他Activity不会置于这个新的Task中
阅读(310) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~