android 启动Activity的四种模式 1:standard,2:singleTop,3:singleTask,4:singleInstance
第一种是默认的启动模式,并没有什么特别:写个例子说明一切(talk is cheep )
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lanchmodetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.lanchmodetest.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>
<activity
android:name="com.example.lanchmodetest.SecondActivity"
/>
</application>
</manifest>
这里是第一种启动模式。代码部分:
package com.example.lanchmodetest;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("FirstActivity", this.toString());
}
public void start(View view)
{
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
}
布局文件就不贴了,就是写一个触发事件onClick,这里我们启动自己,日志如下:
04-14 13:33:30.560: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@1a7acc8b
04-14 13:33:31.730: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@3905675
04-14 13:33:32.080: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@243d7a4f
04-14 13:33:32.420: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@2620c699
04-14 13:33:32.900: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@233925d3
04-14 13:33:33.280: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@d367e7d
04-14 13:33:33.620: E/FirstActivity(1745): com.example.lanchmodetest.MainActivity@be39304
我点击多次,发现创建多个MainActivity实例,依次点返回才会被销毁。
2:singleTop 我们修改
android:launchMode="singleTop",然后看日志:
04-14 13:37:41.190: E/FirstActivity(10153): com.example.lanchmodetest.MainActivity@2da94096
点击多次,只生成一个实例,点击返回,立马销毁。我们不启动自己,启动第二个activity 试试,startActivity(new Intent(MainActivity.this,SecondActivity.class));
SecondActivity 代码如下:
package com.example.lanchmodetest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
public class SecondActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Log.e("SecondActivity", this.toString());
}
public void test(View view)
{
startActivity(new Intent(SecondActivity.this,MainActivity.class));
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("SecondActivity", this.toString()+"onDestroy");
}
}
点击进入第二个Activity ,在点击回到第一个页面,我们发现MainActivity 还是创建了。
04-14 13:39:22.780: E/SecondActivity(10897): com.example.lanchmodetest.SecondActivity@257b385d
04-14 13:39:25.890: E/FirstActivity(10897): com.example.lanchmodetest.MainActivity@3b13b9f7
04-14 13:39:29.250: E/SecondActivity(10897): com.example.lanchmodetest.SecondActivity@2d27ae01
也就是说:singleTop模式下,如果启动的activity已经在栈顶,他不会再启动新的Activity了,否则还是会创建新的Activity。
3:singleTask ,第三种就是能满足有些界面不要创建多次的需求了,修改 android:launchMode="singleTask"
</pre><pre name="code" class="html">04-14 13:47:53.590: E/SecondActivity(17552): com.example.lanchmodetest.SecondActivity@df040e1
04-14 13:47:54.190: E/SecondActivity(17552): com.example.lanchmodetest.SecondActivity@198986db
04-14 13:47:54.260: E/SecondActivity(17552): com.example.lanchmodetest.SecondActivity@df040e1onDestroy
04-14 13:47:55.130: E/SecondActivity(17552): com.example.lanchmodetest.SecondActivity@198986dbonDestroy
从日志来看,这种模式下不会新的实例,并且会把此实例之上的Activity统统出栈!
4:singleInstance , 修改android:launchMode="singleInstance"
04-14 13:58:13.390: E/FirstActivity(23182): com.example.lanchmodetest.MainActivity@7be1591
04-14 13:58:14.920: E/SecondActivity(23182): com.example.lanchmodetest.SecondActivity@1ec5ec0b
04-14 13:58:28.130: E/SecondActivity(23182): com.example.lanchmodetest.SecondActivity@1ec5ec0bonDestroy
经过前面三个,这个倒好理解了,这个也不会创建新的实例,但是
启动一个新的返回栈来管理这个Activity,也就是说,当我们在SecondActivity里启动MainActivity时候,从新启动新的返回栈来管理此Activity,而且SecondActivity不会被销毁,除非你点击了返回。