Thursday, April 18, 2013

Activity


There are three activities and on each of them there are four buttons. When you are in one activity you can go to either one of the other ones. For going to another activity you have two buttons (one that goes without any flags and the other one that has the FLAG_ACTIVITY_REORDER_TO_FRONT).

Each time an activity is created, for intents are created (two for each of the other activities without any flags and two with the flags). When a button is pressed, it will start an activity with the proper intent depending on the text of the button.


  

  Button bSecondWithFlag = (Button) findViewById(R.id.button1);
  Button bThirdWithFlag = (Button) findViewById(R.id.button2);
  Button bSecondWithoutFlag = (Button) findViewById(R.id.button3);
  Button bThirdWithoutFlag = (Button) findViewById(R.id.button4);
  final Intent i2withFlag = new Intent(this, SecondActivity.class);
  i2withFlag.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  final Intent i3withFlag = new Intent(this, ThirdActivity.class);
  i3withFlag.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  final Intent i2withoutFlag = new Intent(this, SecondActivity.class);
  final Intent i3withoutFlag = new Intent(this, ThirdActivity.class);
  bSecondWithoutFlag.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
   startActivity(i2withoutFlag);
   }
  });
  bSecondWithFlag.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
   startActivity(i2withFlag);
   }
  });


There is the drawing with the activities started without any flags:

Description:

Every time an activity is created without a flag, another instance of that activity is created and put on top of the stack. All the other activities that were on the stack are just pushed back one position.



There is the picture with the activity stack when the activities were started with the flag:

Description:

Each time you start an activity with the FLAG_ACTIVITY_REORDER_TO_FRONT it will look through the stack to find an activity that is of the same type. If it finds one, it will resume that one and move it to the top of the stack. If it doesn't find any activity of that type, it will just create one and place it on top of the stack.



The difference between the two examples:
In the first case, the application will just add new instances of the activities each time you start another one without the flag, while in the second case it can also move one activity that was already started to the top and you can use that one without creating a new one.

Optional 1:

After we created some activities without the flag, it is obvious that in the stack could be more activities that are of the same type. When we try to start one more time one activity that is already on the stack but this time with the flag, it will look through the stack and move to the top the last instance of that activity that was pushed to the stack.

Optional 2:

If the same activity was created before, it will invoke the last instance of that activity that is on the stack (the last activity that was invoked before).
If it is the first time that this type of activity is created, it will just create a new instance of the activity.




No comments:

Post a Comment