-1

onBackPressed button my application not properly close. Its again registered my application in background. Its not properly close from background.please tell me how can I close properly

public void onBackPressed() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do you want to exit from application ?");

    builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {



prefProviderWrapper.setPreferenceBooleanValue(PreferencesWrapper.HAS_BEEN_QUIT, true);
    moveTaskToBack (false);
Context context= getApplicationContext();
Communicator comm= new Communicator(context);
Activity app=getParent();
comm.CloseApplication(app);
    finish(); 


}
});
svrushal
  • 1,612
  • 14
  • 25

3 Answers3

1

set launchmode ="singleTask" in manifest file for your activity and invoke YourActvity.this.finish() and System.exit(0) inside onBackPressed().

Asha Soman
  • 1,846
  • 1
  • 18
  • 28
1

Generally it is not advised doing that in an Android application as Romain Guy says: "You should really think about not exiting the application. This is not how Android apps usually work."

But if you still want to achieve this, refer to these topics on stackoverflow which already have very detailed answers and guides for properly closing/quitting/killing your application:

How to close Android application?

Is quitting an application frowned upon?

Close application and launch home screen on Android

Community
  • 1
  • 1
canova
  • 3,965
  • 2
  • 22
  • 39
0

Use this on back button press

@Override
public void onBackPressed() {
    Log.d("CDA", "onBackPressed Called");
    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);
    return;
}
Amardeepvijay
  • 1,626
  • 3
  • 17
  • 47
  • can you say difference between FLAG_ACTIVITY_NEW_TASK and setting the launch mode of an activity to singleTask..? – GvSharma Apr 21 '14 at 06:54
  • FLAG_ACTIVITY_NEW_TASK produces the same behavior as the "singleTask" launchMode, If there's already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task. – Amardeepvijay Apr 21 '14 at 07:06