Activity共有四种启动模式,standard/singleTop/singleTask/singleInstance
在AndroidManifest.xml中在<activity>标签中launchMode指定
<activity android:name=".MainActivity" android:launchMode="standard" > </activity>
1、standard模式
standard是Activity的默认启动模式。 Android是通过返回栈来管理Activity,在standard模式下,每当启动一个新的activity,系统不管这个activity是否存在返回栈中,每次启动都会有一个该activity的新的实例。
package sunny.example.standardactivity; import android.support.v7.app.ActionBarActivity; import android.widget.Button; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.view.View.OnClickListener; import android.content.Intent; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView standard = (TextView)findViewById(R.id.standard); String string = "MainActivity" ; Log.d("MainActivity",this.toString()); standard.setText(string); Button standardB = (Button)findViewById(R.id.standardB); standardB.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,MainActivity.class); startActivity(intent); } }); } } //图见《Android第一行代码》P69 //AndroidManifest.xml <activity android:name=".MainActivity" android:label="@string/app_name" android:launchMode="standard" <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
2、singleTop
与1不同,此模式下,在启动Activity的时候如果发现返回栈的栈顶已经是该Activity,则认为可以直接使用,不用再创建Activity实例。若仅将1的MainActivity的launchMode改为singleTop,则只会产生一个实例,不论按多少次Button。此时仅按一次Back键就可以退出程序。但若MainActivity未处于栈顶,再次启动时还是会创建新的实例,以下即为第二种情况,当点击Button进入SecondActivity,再点击Button进入MainActivity,此时会在创建一个MainActivity实例,因为在由SecondActivity跳到MainActivity时,此时的栈顶是SecondActivity。
//MainActivity.java package sunny.example.singletopactivity; import android.support.v7.app.ActionBarActivity; import android.widget.Button; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.view.View.OnClickListener; import android.content.Intent; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView standard = (TextView)findViewById(R.id.standard); String string = "MainActivity"; standard.setText(string); Button standardB = (Button)findViewById(R.id.standardB); standardB.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } } //SecondActivity.java package sunny.example.singletopactivity; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; import android.content.Intent; import android.widget.TextView; public class SecondActivity extends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.second); TextView secondTextView = (TextView)findViewById(R.id.secondTextView); String sencondTextView = "SecondActivity"; secondTextView.setText(sencondTextView); Button toMainActivity = (Button)findViewById(R.id.toMainActivity); toMainActivity.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(SecondActivity.this,MainActivity.class); startActivity(intent); } }); } }
3、singleTask
与2不同,此模式下,每次启动Activity,系统首先在返回栈中检查是否存在该Activity的实例,如果存在则直接使用该实例,并把在这个Activity之上的所有Activity统统出栈,否则才创建新的实例。
//MainActivity.java package sunny.example.activitysingletask; import android.support.v7.app.ActionBarActivity; import android.widget.TextView; import android.widget.Button; import android.view.View.OnClickListener; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView singleTask = (TextView)findViewById(R.id.singleTask); String text = "MainActivity"; singleTask.setText(text); Button toSecond = (Button)findViewById(R.id.toSecondActivity); toSecond.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } @Override public void onRestart(){ super.onRestart(); TextView restart = (TextView)findViewById(R.id.restart); String text = "SecondActivityRestart"; restart.setText(text); } } //SecondActivity.java package sunny.example.activitysingletask; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Button; public class SecondActivity extends ActionBarActivity{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.second); TextView singleTaskT = (TextView)findViewById(R.id.singleTaskT); String text = "SecondActivity"; singleTaskT.setText(text); Button toMainActivity = (Button)findViewById(R.id.toMainActivity); toMainActivity.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent(SecondActivity.this,MainActivity.class); startActivity(intent); } }); } @Override public void onDestroy(){ super.onDestroy(); TextView destroy = (TextView)findViewById(R.id.destroy); String text = "SecondActivityDestroy"; destroy.setText(text); } }
如上图,当从SecondActivity返回时MainActivity执行了Restart,说明没有创建新的实例。
4、singleInstance
不同于以上三种,Activity指定为该模式时系统会启用一个新的栈来管理这个Activity。应用场景:Activity1与Activity3共享Activity2的实例,此时可以指定Activity2为singleInstance模式。解决了共享实例的问题。
//MainActivity.java package sunny.example.activitysingleinstance; import android.support.v7.app.ActionBarActivity; import android.widget.TextView; import android.widget.Button; import android.view.View.OnClickListener; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView singleTask = (TextView)findViewById(R.id.singleInstance); String text = "MainActivity"+getTaskId(); singleTask.setText(text); Button toSecond = (Button)findViewById(R.id.toSecondActivity); toSecond.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } }); } @Override public void onRestart(){ super.onRestart(); TextView restart = (TextView)findViewById(R.id.restart); String text = "SecondActivityRestart"; restart.setText(text); } } //SecondActivity.java package sunny.example.activitysingleinstance; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; import android.content.Intent; import android.widget.TextView; public class SecondActivity extends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.second); TextView secondTextView = (TextView)findViewById(R.id.secondTextView); String sencondTextView = "SecondActivity" + getTaskId(); secondTextView.setText(sencondTextView); Button toMainActivity = (Button)findViewById(R.id.toMainActivity); toMainActivity.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(SecondActivity.this,ThirdActivity.class); startActivity(intent); } }); } } //ThirdActivity.java package sunny.example.activitysingleinstance; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.widget.TextView; public class ThirdActivity extends ActionBarActivity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.third); TextView third = (TextView)findViewById(R.id.third); String text = "ThirdActivity"+getTaskId(); third.setText(text); } } //ActivityManifest.xml <activity android:name=".SecondActivity" android:launchMode="singleInstance"> </activity>
由图可见,SecondActivity的TaskId与MainActivity、ThirdActivity不同,SecondActivity存放在一个单独的返回栈里。MainActivity与ThirdActivity在同一个栈里,此时由ThirdActivity按返回键会返回到MainActivity,再按返回键到栈顶SecondActivity,即ThirdActivity-->MainActivity-->SecondActivity--退出程序。