activity启动模式之singleTop
Posted 范仁义
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了activity启动模式之singleTop相关的知识,希望对你有一定的参考价值。
activity启动模式之singleTop
一、简介
二、设置方法
在androidManifest.xml中将要设置为singleTop启动模式的页面进行配置
<activity android:name="activityLaunchSingleTop.ActivityB2" android:launchMode="singleTop"></activity>
三、代码实例
效果图:
代码:
activityLaunchSingleTop.MainActivity
1 package activityLaunchSingleTop; 2 3 4 5 6 import com.example.activityLaunchSingleTop.R; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 15 16 17 public class MainActivity extends Activity{ 18 private Button btn_goB1;//创建一个button对象 19 private Button btn_goB2;//创建一个button对象 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState);//父类操作 22 setContentView(R.layout.activity_main);//引入名为activity_main的界面 23 btn_goB1=(Button) findViewById(R.id.btn_goB1);//找id为btn_openActivity的button 24 btn_goB1.setOnClickListener(new OnClickListener() {//设置button点击监听 25 26 @Override 27 public void onClick(View v) {//onclick事件 28 // TODO Auto-generated method stub 29 Intent intent=new Intent();//初始化intent 30 intent.setClass(MainActivity.this,MainActivity.class);//连接 31 startActivity(intent);//打开activity 32 } 33 }); 34 35 btn_goB2=(Button) findViewById(R.id.btn_goB2);//找id为btn_openActivity的button 36 btn_goB2.setOnClickListener(new OnClickListener() {//设置button点击监听 37 38 @Override 39 public void onClick(View v) {//onclick事件 40 // TODO Auto-generated method stub 41 Intent intent=new Intent();//初始化intent 42 intent.setClass(MainActivity.this,ActivityB2.class);//连接 43 startActivity(intent);//打开activity 44 } 45 }); 46 } 47 }
activityLaunchSingleTop.ActivityB2
1 package activityLaunchSingleTop; 2 3 4 5 6 import com.example.activityLaunchSingleTop.R; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.text.InputFilter.LengthFilter; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.Button; 15 import android.widget.Toast; 16 17 18 19 public class ActivityB2 extends Activity{ 20 private Button btn_goB1;//创建一个button对象 21 private Button btn_goB2;//创建一个button对象 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState);//父类操作 24 setContentView(R.layout.activity_b2);//引入名为activity_main的界面 25 btn_goB1=(Button) findViewById(R.id.btn_goB1);//找id为btn_openActivity的button 26 btn_goB1.setOnClickListener(new OnClickListener() {//设置button点击监听 27 28 @Override 29 public void onClick(View v) {//onclick事件 30 // TODO Auto-generated method stub 31 Intent intent=new Intent();//初始化intent 32 intent.setClass(ActivityB2.this,MainActivity.class);//连接 33 startActivity(intent);//打开activity 34 } 35 }); 36 37 btn_goB2=(Button) findViewById(R.id.btn_goB2);//找id为btn_openActivity的button 38 btn_goB2.setOnClickListener(new OnClickListener() {//设置button点击监听 39 40 @Override 41 public void onClick(View v) {//onclick事件 42 // TODO Auto-generated method stub 43 Intent intent=new Intent();//初始化intent 44 intent.setClass(ActivityB2.this,ActivityB2.class);//连接 45 startActivity(intent);//打开activity 46 } 47 }); 48 } 49 @Override 50 protected void onNewIntent(Intent intent) { 51 // TODO Auto-generated method stub 52 super.onNewIntent(intent); 53 Toast.makeText(this, "onNewIntent", Toast.LENGTH_SHORT).show();; 54 } 55 }
/activityLaunchSingleTop/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.activityLaunchSingleTop" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application 11 android:allowBackup="true" 12 android:icon="@drawable/ic_launcher" 13 android:label="@string/app_name" 14 android:theme="@style/AppTheme" > 15 <activity 16 android:name="activityLaunchSingleTop.MainActivity" 17 android:label="@string/app_name" > 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 25 <activity android:name="activityLaunchSingleTop.ActivityB2" android:launchMode="singleTop"></activity> 26 </application> 27 28 </manifest>
/activityLaunchSingleTop/res/layout/activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tV_B1" 9 android:layout_width="match_parent" 10 android:layout_height="96dp" 11 android:text="@string/tV_B1" 12 android:textAppearance="?android:attr/textAppearanceLarge" /> 13 14 <Button 15 android:id="@+id/btn_goB1" 16 android:layout_width="match_parent" 17 android:layout_height="50dp" 18 android:layout_weight="0.00" 19 android:text="@string/btn_goB1" /> 20 21 <Button 22 android:id="@+id/btn_goB2" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:text="@string/btn_goB2" /> 26 27 </LinearLayout>
/activityLaunchSingleTop/res/layout/activity_b2.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/tV_B2" 9 android:layout_width="match_parent" 10 android:layout_height="96dp" 11 android:text="@string/tV_B2" 12 android:textAppearance="?android:attr/textAppearanceLarge" /> 13 14 <Button 15 android:id="@+id/btn_goB1" 16 android:layout_width="match_parent" 17 android:layout_height="50dp" 18 android:layout_weight="0.00" 19 android:text="@string/btn_goB1" /> 20 21 <Button 22 android:id="@+id/btn_goB2" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:text="@string/btn_goB2" /> 26 27 </LinearLayout>
以上是关于activity启动模式之singleTop的主要内容,如果未能解决你的问题,请参考以下文章