Clean up a task and start a new action

I don’t know why it is so hard to understand. I have my main activity when at startup it is checked whether it will be opened for the first time. If so, it closes the main action and opens the install / enter operation with FLAG_ACTIVITY_NEW_TASK . The setup process consists of three actions (A, B, and C). At the end of the C action, how can I clear it, and the setup task that contains A, B and C, and run the main action again. I tried adding FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP to the main activity of Intent , but when I press BACK, it returns to activity C of the installation process. How can I clear the task of actions A, B and C when C finishes and starts the main one? Thanks!

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

+6
source share
4 answers

FLAG_ACTIVITY_CLEAR_TOP will clear the actions that it has from one instance of the activity. Here, in your case, all your actions have different instances, so FLAG_ACTIVITY_CLEAR_TOP will not work. To clear the task, create an instance of Activity in each of your actions and assign this instance of 'this' for your onCreate method for each action. whenever you want to clear your jus task call instance.finish (). and start the work you want.

+2
source

In fact, this can be achieved using 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 do not leak in this way.

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

+1
source

I answered a similar question here

As Mudasir says in his comment, just finish() your actions immediately after starting a new one.

0
source
 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 the entire task of the current activity, and then call instanceOfA.finish (); instanceOfB.finish (); instanceOfC.finish ();

0
source

Source: https://habr.com/ru/post/906179/


All Articles