Api demo源码学习(11)--App/Activity/Reorder Activity
本节实现四个Activity中进行跳转,比较简单,唯一需要注意的是调用语句
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
它的功能是,如果跳转后的Activity已存在,则不是新创建一个activity,而是回到原来已存在的Activity中。
Api demo中只在第四个Activity中调用了这个语句,效果不明显,如果我们在四个Activity中都添上这条语句,那无论我们点击多少次跳转(>=4次),我们按返回键都只需要四次,依次返回四个Activity后就可以退出程序,因为每次跳转不是重新开一个新的Activity,而是将原来已存在的Activity拉到任务栈的顶端上来。
ReorderOnLaunchActivity.java
1 /** Called when the activity is first created. */
2 @Override
3 protected void onCreate(Bundle savedState) {
4 super.onCreate(savedState);
5
6 setContentView(R.layout.reorder_on_launch);
7
8 Button twoButton = (Button) findViewById(R.id.reorder_launch_two);
9 twoButton.setOnClickListener(mClickListener);
10 }
11
12 private final OnClickListener mClickListener = new OnClickListener() {
13 public void onClick(View v) {
14 Intent intent = new Intent(ReorderOnLaunchActivity.this, ReorderTwo.class);
15 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
16 startActivity(intent);
17 //finish();
18 }
19 };
20 }
ReorderTwo.java
1 public class ReorderTwo extends Activity {
2 @Override
3 protected void onCreate(Bundle savedState) {
4 super.onCreate(savedState);
5
6 setContentView(R.layout.reorder_two);
7
8 Button twoButton = (Button) findViewById(R.id.reorder_launch_three);
9 twoButton.setOnClickListener(mClickListener);
10 }
11
12 private final OnClickListener mClickListener = new OnClickListener() {
13 public void onClick(View v) {
14 Intent intent = new Intent(ReorderTwo.this, RecorderThree.class);
15 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
16 startActivity(intent);
17 //finish();
18 }
19 };
20
21 }
RecorderThree.java
1 public class RecorderThree extends Activity{
2 @Override
3 protected void onCreate(Bundle savedState) {
4 super.onCreate(savedState);
5
6 setContentView(R.layout.reorder_three);
7
8 Button twoButton = (Button) findViewById(R.id.reorder_launch_four);
9 twoButton.setOnClickListener(mClickListener);
10 }
11
12 private final OnClickListener mClickListener = new OnClickListener() {
13 public void onClick(View v) {
14 Intent intent = new Intent(RecorderThree.this, RecorderFour.class);
15 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
16 startActivity(intent);
17 //finish();
18 }
19 };
20
21 }
RecorderFour.java
1 public class RecorderFour extends Activity {
2 @Override
3 protected void onCreate(Bundle savedState) {
4 super.onCreate(savedState);
5
6 setContentView(R.layout.reorder_four);
7
8 Button twoButton = (Button) findViewById(R.id.reorder_second_to_front);
9 twoButton.setOnClickListener(mClickListener);
10 }
11
12 private final OnClickListener mClickListener = new OnClickListener() {
13 public void onClick(View v) {
14 Intent intent = new Intent(RecorderFour.this, ReorderTwo.class);
15 intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
16 startActivity(intent);
17 //finish();
18 }
19 };
20
21 }
最后别忘了在AndroidMenifest.xml中注册另外三个Activity。
以上即可。