开发者

How to kill app on button press

I have an app that has a lot of activities. In the "Settings" page ther开发者_开发问答e is a delete account button that is supposed to reset all the saved variables and exit the app. I haven't found a simple solution to exiting the app (eg calling finish only destroys the current activity) What do I call to close the app on a button press (eg when I reopen the the app it should start from the first activity)


Easiest way to do this is to register a BroadcastReceiver in all Activity classes that listens for a specific Intent. When you want to close everything then just fire the matching Intent, and in the BroadcastReceiver in each Activity call finish.


Try System.exit(0), although you're technically suppose to use finish() on all the activities. This does the same, but quickly.


Use this:

Process.killProcess(Process.myPid()); 

Or there is another more safer approach. Just subclass all of your activities, from one parent Activity and keep list of all alive activities and then when necessary close them all using exit():

public class ControlActivity extends Activity     
{     
   private static ArrayList<Activity> activities=new ArrayList<Activity>();      

   @Override     
   public void onCreate(Bundle savedInstanceState)     
   {         
       super.onCreate(savedInstanceState);         
       activities.add(this);     
   }      

   @Override     
   public void onDestroy()     
   {         
       super.onDestroy();         
       activities.remove(this);         
       if(activities.size()==0) //last activity              
           //release resources and so on     
   }   

   //close all activities, when necessary
   public static void exit()
   {
      for(Activity activity:activities)
          activity.finish();
   }
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜