Closing of Activity in Android

2019-06-09 14:46发布

问题:

I have 3 Activities

Activity1 >>Activity2 >>Activity3

In activity3 I'm finishing the activity and calling system.exit(0) to close my complete app

  1. should close background music
  2. close the activity
  3. destroy the appication

Condition is like this

  1. I've a timer in this activity3. On the OnFinish of the timer I should start activity2. I can't call NoHistory=true on activity2 because of the above condition

Any Help will be appreciated...

回答1:

I think the following LINK will help you which is related to closing of app..



回答2:

other then all above links provided in comments there is one more approach if you are using the BaseActivity concept ............

1- just keep a Boolean variable any where at global place in the applcaion like in Application class. boolean finishApp = true; (not keep this static)

2- either override the onRestart/onStart in BaseAcivity ( or in each activity :( ) as

onRestart(){
   super.onRestart();
    if(isFinishApp()){
       finish()
   }
} 

3- on Button click just set finishApp true and finish current App.



回答3:

You have two Options, by you can exit the app, First you can close all the activities over HomeScreen.

Intent intent=new Intent(this, HomeClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Else you can implement a sub activity implementation, to implement so, you need to start each activity for result, and in event of exit app, do following:

In Subactivity:

intent.putExtra("ACTION", "Exit);
setResult(RESULT_OK, intent);
finish();

and in onActivityResult check for result, if it has Action with Exit value, then set Result and finish that activity.