Android: clear all backstack operations and then complete current activity

I have various actions in my application, and the stream is very complex. I want to do this, as soon as the USB device is connected, I want to clear and finish the actions of the back stack, and then complete the current activity and System.exit (0) to close the application.

I have already implemented a usb device listener. I want to know how to clear and exit the stack (if any, it will not have any backstack operations every time), and then complete the current one.

In addition, if my activity A is on top, it has 2 actions (B, C) in the feedback stack. Now, if activity A is running in the background and plugged into a USB port, only action A will listen to it correctly? (I have a usb receiver implemented in every action.)

How can I achieve this without my application crashing?

thanks

+5
source share
2 answers

there is a finishAffinity() method that will complete the current activity and all parental actions, but only works in Android 4.1 or higher

A source

finishAffinity() will complete this operation, as well as all actions immediately below it in the current task with the same proximity

If you want to use all API levels
in one of your actions.

 Intent intent = new Intent(this, YourActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // this will clear all the stack intent.putExtra("Exit me", true); startActivity(intent); finish(); 

Then in the YourActivity onCreate () method add this to complete the operation

 setContentView(R.layout.your_layout); if( getIntent().getBooleanExtra("Exit me", false)){ finish(); return; // add this to prevent from doing unnecessary stuffs } 
+6
source

In the application I made, I put:

 if(Globals.isExit){ finish(); } 

In every onResume () method in every action.

Globals is a class declared as an .xml manifest. The Global class has a logical name: exit.

In the parameters menu of all actions, enable the exit parameter, which sets the value of Globals.exit to true and calls finish ()

Then all the actions that remain incomplete are completed.

Not sure if you will need to use the intention to clean the back or complete it for you. Sorry if I'm wrong about this bit.

Sorry for the bad typing, I'm on the phone.

0
source

All Articles