0

How does one quit an app from a non-activity/fragment? I have a class that handles events, and if a certain event happens, i wish to quit my app. I normally allow users to quit the app by doing the following:

getActivity().finish();
System.exit(0);

However, I can't call getActivity() from a class that's not an activity or fragment.

Thanks.

user1118764
  • 9,255
  • 18
  • 61
  • 113
  • What is the "non-activity"? Is it a service? You can define your app as a Broadcast receiver and then broadcast some message from the service. Then in `onReceive` of the activity use the code above (by the way, you don't need `System.exit(0)`) – Aleks G Jun 09 '14 at 09:38
  • instead getActivity(), insert your Activity`s name will do it. – Umit Kaya Jun 09 '14 at 09:40
  • Well if it was allowed to kill an app without any reference to its Context, then it could be easily be misused. I guess you need to have a reference to the activity on top of your activity stack to do this. – Parth Kapoor Jun 09 '14 at 09:41
  • Pass in a reference to the activity, and call finish on it. – Gabe Sechan Jun 09 '14 at 09:48
  • or simply pass a valid context and do your work – Illegal Argument Jun 09 '14 at 09:53
  • Gabe, do you mean something like what Matej answered below? Illegal Argument, I have a context in the class, what can I do with it to finish the app/activity? – user1118764 Jun 09 '14 at 09:58

0 Answers0