-1

How I have app where I have 2 activities and on second I have quit button which passes me to first activity. What should I do if I would like to kill all process after click this button? I mean that I will back to my previously activity with stop all tasks in background

Jezdi78
  • 21
  • 4
  • 2
    Does this answer your question? [How to quit android application programmatically](https://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically) – Sagar Jan 19 '21 at 14:03

3 Answers3

0

Button button = (Button) findViewById(R.id.btn1); button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
         activity.finish(); // to move out of activity
           // to move out of the app
           //activity.finishAffinity();
    
    }
});
}

}

Antier Solutions
  • 1,326
  • 7
  • 10
0

you can just call finishAffinity(); in your click event

0

Is the correct scenario to close the app by a button / by user action?

You should have a really well scenario to use this case.

Is against application processing principles.

Android framework only kills applications only if the resources are needed. Also has extra benefits: App startup is faster if the process is alive.

 Activity.finish(); // will finish the current activity.
 Activity.finishAffinity()  //removes  Activitys belonging to a specific application from the current task

If it's the case you want to "exit" the application I would recommend an approach where you "simulate" closing the application. (you bring the HomePage in front)

Intent homePageIntent= new Intent(Intent.ACTION_MAIN);
homePageIntent.addCategory( Intent.CATEGORY_HOME );
homePageIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
startActivity(homePageIntent); 

Oh another tip: Before asking a question try to search your question on Google. You could have noticed that your question was asked multiple times.

A great example.

Sz-Nika Janos
  • 811
  • 6
  • 19