第一步
,大家要知道Activity具有哪四种启动模式,他们分别是standard模式,singleTop模式,single Task模式以及singlenstance模式。
其中standard模式是Activity的默认启动模式,在不指定启动模式的情况下,所有Activity使用的都是standard模式。怎么观察此模式下的效果呢,代码如下:
创建一个Activity
ackage cn.ezu.bzu.myapplication; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class FirstActivity extends AppCompatActivity { private Button btnClick; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_layout); Log.d("FirstActivity", this.toString()); btnClick = (Button) findViewById(R.id.btnClick); btnClick.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FirstActivity.this, FirstActivity.class); startActivity(intent); } }); } public void welcome(View view){ Toast.makeText(this,"welcome",Toast.LENGTH_LONG).show(); } }
定义的界面
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="toFirst" android:drawableLeft="@mipmap/ic_launcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnClick" /> </LinearLayout>这个模式的满足先进后出的原则。第二个模式叫做singleTop模式,他所需要修改的就是将manifests下的AndroidMainfest.xml中的launchMode修改为singletop。 这个模式与standard想似,但是当启动的Activity已经位于栈顶时,则直接使用它而不创建新的实例。如果Activity没有位于栈顶,则创建一个新的实例位于栈顶。第三个模式叫做singleTask模式,他所需要修改的是将manifests下的AndroidMainfest.xml中的launchMode修改为singleTask。并且在FirstActivity下添加一个onRestart的方法,即:
@Override
protected void onRestart() {
super.onRestart();
Log.d("FirstActivity","onRestart()");
同理在SecondActivity下也要添加一个onDestroy()方法,即:
protected void onDestroy() {
super.onDestroy();
Log.d("SecondActicity","onDestory");
这个模式下每次启动Activity时,系统会检查栈中是否存在该Activity实例,如果发现已经存在则直接使用该实例,并将当前Activity之上的所有Activity出栈,如果没有发现则创建一个新的实例。
最后一个启动模式是singlelnstance模式,他所需要修改的是将log.d命令行修改为Log.d("FirstActivity", "Task id is"+getTaskId());,然后将所关联的两个布局稍作修改以便观察即可。
不同于以上三个模式,这个模式下的Activity会启动一个新的人物栈来管理这个Activity。
下面给大家举一个Activity的实例代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_third" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.firstactivity.ThirdActity"> <TextView android:text="第三个Activity" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="19dp" /> <Button android:onClick="Second" android:text="to Second" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Second" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:onClick="First" android:text="to First" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/First" android:layout_above="@+id/Second" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginBottom="58dp" /> </RelativeLayout>