6

I don't know why this is so difficult to figure out. I have my main activity that, when launched, checks if this is the first time it's been opened. If it is, then it closes the main activity and opens the setup/introduction activity with FLAG_ACTIVITY_NEW_TASK. The setup process consists of three activities (A, B, and C). At the end of activity C, how do I get it to clear and the setup task that contains A, B, and C and start the main activity again. I've tried adding FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP to main activity Intent but when I press BACK, it returns to activity C of the setup process. How do I get it to clear the task of activities A, B, and C when C finishes and starts the main? Thanks!

I'm building in Android 1.6 (API 4), so some of the Activity flags may be limited.

Brian
  • 7,955
  • 16
  • 66
  • 107
  • Finish the Activity C when you are proceeding to Main Activity. – Mudassir Jan 17 '12 at 06:07
  • clearing the flag only takes one activity off the stack, no? – L7ColWinters Jan 17 '12 at 06:10
  • @Mudassir If I just finish Activity C, then when I press BACK from the main activity, it just goes to Activity B. – Brian Jan 17 '12 at 06:12
  • I want to provide the user with the ability to go back a step if they need to change something in the setup. The whole setup process is done after they click "Finish" on the last step; at that point, it should just destroy the entire stack of setup activities. – Brian Jan 17 '12 at 06:21
  • Thats what you will do in the `onBackPressed()` method. Start the previous Activity (read step). – Mudassir Jan 17 '12 at 06:23
  • Alright well it's going to be an ugly work around because the activity transition animations are not going to make sense. But are you telling me that such a simple task/activity management feature like this can't be achieved in Android? – Brian Jan 17 '12 at 06:26
  • Are your transitions not working? – Mudassir Jan 17 '12 at 06:35
  • @Brian: +1 for the question.. BTW, are you trying to implement something like the End User License Agreement? – Ghost Jan 17 '12 at 06:41
  • @Ghost: Well it's kind of similar. The whole idea is that I want to close a task (stack of activities) when I'm done using them, then start a new activity. Is it really that difficult? – Brian Jan 17 '12 at 07:05
  • @Mudassir: My transitions are working fine, what I meant is that when an activity finishes the default animation is for it to slide to the right (as if you were going back to the original activity). I'm just saying that it wouldn't "look" like the user pressed the BACK button because it would actually be starting an activity, so the animation would look like a new activity is coming in instead of "going back". I hope you understand what I mean. – Brian Jan 17 '12 at 07:08
  • I got it. But Android doesn't allow us to finish multiple or other activities. – Mudassir Jan 17 '12 at 07:19
  • @Mudassir: Well if it's not too convoluted and complicated, I'll give a try. I don't want to make this simple idea be complicated if you know what I mean. – Brian Jan 17 '12 at 07:38
  • @Brian: Sorry if I kept you waiting. Please see the answer below. – Mudassir Jan 17 '12 at 08:18
  • @Brian: I don't really know if it's that difficult. I haven't really tried it but I think you can give this a shot - http://bees4honey.com/blog/tutorial/adding-eula-to-android-app/ – Ghost Jan 17 '12 at 09:56
  • Have a look on @Arun MG's answer. That is a more straight forward way. – Mudassir Jan 17 '12 at 10:29

4 Answers4

2

FLAG_ACTIVITY_CLEAR_TOP will clear activities it it is of the same activity instance. Here in your case all your activities are of different instances so FLAG_ACTIVITY_CLEAR_TOP won't work. To clear your task, create an Activity instance in each of your activity and assign that instance 'this' on your onCreate method of every activity. whenever you want to clear your task jus call that instance.finish(). and start the activity that you want.

Arun
  • 1,658
  • 1
  • 14
  • 21
  • 1
    Can this finish all the THREE activity at once..? – Mudassir Jan 17 '12 at 07:17
  • its not once, see you have Activity A, B ,C. To finish all these at once, u just need to do something similar below activityA_instace.finish(); activityB_instace.finish(); activityC_instace.finish(); also make sure your instances are public static from the activity where you have declared. If it is so, you can actually clear your task from anywhere by the way i just showed above – Arun Jan 17 '12 at 08:29
  • What exactly do you mean by instance? Can you please explain using some code? – Mudassir Jan 17 '12 at 08:51
1

Actually this can be achieved with startActivityForResult

public class A extends Activity {
   public onButtonClick() {
      startActivityForResult(new Intent(this, B.class), 0);
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {
         finish();
      }
   }
}

public class B extends Activity {
   public onButtonClick() {
      startActivityForResult(new Intent(this, C.class), 0);
   }
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {
         setResult(RESULT_OK);
         finish();
      }
   }
}

public class C extends Activity {
   public onButtonClick() {
      setResult(RESULT_OK);
      finish();
   }
}

I think this is the right way, you don't leak anything this way.

PS: I know this is old post, but maybe someone will find this useful.

sidon
  • 1,434
  • 1
  • 17
  • 30
0

I answered a similar question here

As Mudassir says in their comment just finish() your activities immediately after starting a new one.

Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • No, I want to let the user to be able to go back a step if they need to during the setup process. All I want to is to finish the stack of setup activities when the last step is done. – Brian Jan 17 '12 at 06:20
0
class A extends Activity { 
public static Activity instanceOfA = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instanceOfA = this;
}
}

class b extends Activity { 
public static Activity instanceOfB = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instanceOfB = this;
}
}


class c extends Activity { 
public static Activity instanceOfC = null;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instanceOfC = this;
}
}

Now suppose you want to clear all the task from your current activity, then call instanceOfA.finish(); instanceOfB.finish(); instanceOfC.finish();

Arun
  • 1,658
  • 1
  • 14
  • 21
  • 9
    This is unacceptable. You're leaking all the activities and all the resources (view hiarerchies, *images*) associated with them. – Jonas Alves Dec 03 '12 at 16:59
  • Something similar to this [answer](http://stackoverflow.com/a/3008684/269876) will suit you better. As @JonasAlves points out, you are leaking all those activities by using statics. Instead, come up with a message that you can broadcast when the setup completes, and have each setup activity listening for the broadcast. When they receive the setup complete notification, they can finish themselves, and no statics are needed to achieve this. – Lo-Tan Mar 27 '13 at 20:39
  • 2
    Static references to activities means your views are remaining in memory and are not recycled, resulting in a memory leak. – George Mar 29 '13 at 14:07