Reputation: 8085
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.
Upvotes: 6
Views: 5283
Reputation: 1482
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.
Upvotes: 1
Reputation: 1668
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();
Upvotes: 0
Reputation: 1668
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.
Upvotes: 2