Closing of Activity in Android

2019-06-09 14:15发布

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...

3条回答
做个烂人
2楼-- · 2019-06-09 14:52

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-06-09 15:09

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

查看更多
别忘想泡老子
4楼-- · 2019-06-09 15:11

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.

查看更多
登录 后发表回答