Android中Activity四种加载模式
Activity四种加载模式
那么Activity为什么需要制定加载模式呢?
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); this.setContentView(ll); TextView tv = new TextView(this); tv.setText("Activity为: " + this.toString() + "\n" + ", Task ID 为:" + this.getTaskId()); Button button = new Button(this); button.setText("启动Activity"); ll.addView(tv); ll.addView(button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); } } ); } }
这种模式创建的Activity也是和standard一样直接放栈顶,不同之处在于,singleTop模式在启动Activity之前会检测当前栈顶Activity是否和当前需要启动的Activity是同一类,如果是同一类,系统将直接复用已经拥有的Activity,不是同一类则会和standard模式一样创建一个Activity放到栈顶.
代码和上面的一模一样,需要在AndroidManifest.xml中加入 android:launchMode="singleTop"
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); this.setContentView(ll); TextView tv = new TextView(this); tv.setText("Activity为: " + this.toString() + "\n" + ", Task ID 为:" + this.getTaskId()); Button button = new Button(this); button.setText("启动Activity"); ll.addView(tv); ll.addView(button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } } ); } } public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); this.setContentView(ll); TextView tv = new TextView(this); tv.setText("Activity为: " + this.toString() + "\n" + ", Task ID 为:" + this.getTaskId()); Button button = new Button(this); button.setText("启动Activity"); ll.addView(tv); ll.addView(button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SecondActivity.this,MainActivity.class); startActivity(intent); } } ); } }
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); this.setContentView(ll); TextView tv = new TextView(this); tv.setText("Activity为: " + this.toString() + "\n" + ", Task ID 为:" + this.getTaskId()); Button button = new Button(this); button.setText("启动Activity"); ll.addView(tv); ll.addView(button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } } ); } } public class SecondActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); this.setContentView(ll); TextView tv = new TextView(this); tv.setText("Activity为: " + this.toString() + "\n" + ", Task ID 为:" + this.getTaskId()); Button button = new Button(this); button.setText("启动Activity"); ll.addView(tv); ll.addView(button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction("org.crazyit.intent.action.CRAZYIT_ACTION"); startActivity(intent); } } ); } } <activity android:name="com.example.dada.testapplication.SecondActivity" android:launchMode="singleInstance" android:exported="true" android:label="@string/title_activity_second" > <intent-filter> <action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。