Android中Activity的四大启动模式实例讲解

作为Android四大组件之一,Activity可以说是最基本也是最常见的组件,它提供了一个显示界面,从而实现与用户的交互,作为初学者,必须熟练掌握。今天我们就来通过实验演示,来帮助大家理解Activity的四大启动模式。

演示效果如下:

Android中Activity的四大启动模式实例讲解

第一步:实验前准备,相关配置文件以及Activity的建立 (1)AndroidMainfest.xml配置文件

1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.administrator.intentflags"> 4 <application 5 android:allowBackup="true" 6 android:icon="@mipmap/ic_launcher" 7 android:label="@string/app_name" 8 android:supportsRtl="true" 9 android:theme="@style/AppTheme"> 10 <activity android:name=".MainActivity"> 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 14 <category android:name="android.intent.category.LAUNCHER" /> 15 </intent-filter> 16 </activity> 17 //另外两个activity 18 <activity android:name=".Main2Activity"> </activity> 19 <activity android:name=".Main3Activity"></activity> 20 </application> 21 </manifest>

(2)layout中三个activity的三个布局文件activity_main.xml(这里演示一个,其余两个只是下面TextView文字不一样)

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/activity_main" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 android:orientation="vertical" 9 tools:context="com.example.administrator.intentflags.MainActivity"> 10 <Button 11 android:text="第一个页面" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:onClick="click1" /> 15 <Button 16 android:text="第二个页面" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:onClick="click2" /> 20 <Button 21 android:text="第三个页面" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:onClick="click3" /> 25 <TextView 26 android:layout_width="match_parent" 27 android:layout_height="match_parent" 28 //其余两个仅文字不一样 29 android:text="第一个页面" 30 android:textSize="50sp"/> 31 </LinearLayout>

(3)java中三个activity的实现代码MainActivity.java(仅演示一个,其他两个完全相同)

1 import android.content.Intent; 2 import android.support.v7.app.AppCompatActivity; 3 import android.os.Bundle; 4 import android.util.Log; 5 import android.view.View; 6 /** 7 * Created by panchengjia on 2016/12/14. 8 */ 9 public class MainActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 //其他两个界面此处引用各自对应的layout文件 15 setContentView(R.layout.activity_main); 16 } 17 public void click1(View view){ 18 Intent intent = new Intent(this,MainActivity.class); 19 startActivity(intent); 20 //记录Taskid用于实验说明 21 Log.i("Tag","页面一taskId:"+getTaskId()); 22 } 23 public void click2(View view){ 24 Intent intent = new Intent(this,Main2Activity.class); 25 startActivity(intent); 26 Log.i("Tag","页面二taskId:"+getTaskId()); 27 } 28 public void click3(View view){ 29 Intent intent = new Intent(this,Main3Activity.class); 30 startActivity(intent); 31 Log.i("Tag","页面三taskId:"+getTaskId()); 32 } 33 }

第二步:standard默认(标准)模式下的演示实验

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/feca4fef650c41045f9ca546e65b8d87.html