1

My application contains 12 activities.Can i exit from the entire application in one click? finish() finishes only a single activity and the previous activity launched. Please help me for fix this problem

Fighter
  • 29
  • 1
  • 9

3 Answers3

0

When starting the activity from which you want to close all activity, add a flag to the intent.

  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

this will make your current activity as the top and thus when you will finish this your task will be accomplished

umesh lohani
  • 162
  • 1
  • 4
  • 13
0

Create static instance of all activities

public class Activity1 extends FragmentActivity {

    private static Activity1 sActivity;

    public static Activity1 getsActivity() {
        return sActivity;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sActivity = this;

}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sActivity = null
    }
}
}

Now create an exit method in your Utility class like and start killing the activity from the higher order, and call it from anywhere inside your app

public static void exitApp(){
    if(Acitivity10.getsActivity() != null){
    Activity10.getsActivity.finish();   
}
    if(Acitivity9.getsActivity() != null){
    Activity9.getsActivity.finish();    
}
-
-
if(Acitvity1.getsActivity()!= null){
    Activty1.getsActivity().finish();
}
}
Abhishek
  • 1,337
  • 10
  • 29
  • Imagine searching in Play store. Everytime you type in a new search it opnes a new search activity. You need to be able to handle multiple activities of the same kind. – Eugen Pechanec Apr 03 '15 at 10:15
  • hmm, so this way we cant handle killing multiple activities of same kind, cz it will only kill the current activity and rest of the activities will remain same – Abhishek Apr 03 '15 at 10:26
  • We could possibly use a collection of WeakReferences. Maybe an ArrayList with WeakReferences with callbacks which would remove the vacated reference from the list so it doesnt grow uncontrollably. – Eugen Pechanec Apr 03 '15 at 10:31
  • exactly, an arraylist will work in that case, but i think for above case it will work fine. Isn't it? – Abhishek Apr 03 '15 at 10:40
  • I dare not say. If OP has only one instance of each activity at one time it should suffice. – Eugen Pechanec Apr 03 '15 at 10:42
-1
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                this);
        alertDialogBuilder.setTitle("Exit App?");
        alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("No",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        })

                .setNegativeButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                moveTaskToBack(true);
                                android.os.Process
                                        .killProcess(android.os.Process
                                                .myPid());
                                System.exit(0);

                            }
                        });