0

I am working on an Android project the requirement is as follows: if any exceptions occur we need to exit the application by showing error message.

I am aware that Android doesn't allow to exit application programmatically so other work around should be to freeze the application that is user should not be able to do anything in the application. But how do I implement this.

Pitty
  • 1,907
  • 5
  • 16
  • 34
user3048027
  • 387
  • 1
  • 5
  • 24
  • http://stackoverflow.com/questions/21130493/how-to-close-android-app-completely/21130567#21130567 – Amresh Mar 24 '14 at 06:29

5 Answers5

5

hey call this function when error occurs its simple

public void close()
{
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
grebulon
  • 7,697
  • 5
  • 42
  • 66
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
3
public void quit() {
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    System.exit(0);
}

(But using system.exit() is a bad approach in android. Better to use finish() )

OR

Intent i= new Intent(getapplicationcontext,TARGETACTIVITY.class);
finish();
startActivity(intent);
grebulon
  • 7,697
  • 5
  • 42
  • 66
Android
  • 8,995
  • 9
  • 67
  • 108
  • 4
    -1 for system.exit() without explaining why it's bad. – Simon Mar 24 '14 at 07:25
  • 4
    i think a android developer must know this and if YOU not knowing then google it.. here we are not for TEACHING.. here just for solving problems and finding solution..@simon – Android Mar 24 '14 at 07:27
  • 3
    You misunderstand me. system.exit() should only be used in special cases. My downvote is because you proposed it as an answer and beginners would not know that it's bad unless you told them. You have now edited the question, so I cancelled my vote. And, I disagree with you about teaching. If you don't teach here, then bad developers are born and the whole Android world suffers. – Simon Mar 24 '14 at 15:45
  • Stack Overflow is a question and answer site for professional and enthusiast programmers. this is this.... – Android Mar 25 '14 at 04:34
  • if i use first solution, broadcast receicers( push ) are working properly which i registered in manifest? – Manikanta Ottiprolu Feb 07 '15 at 06:23
0

try this one:

try{
-------
-----
----
}
Catch (Exception e)
{
exception.printStackTrace();//using alert box to show your exception. 
finish(); //use this one of your Application it will close the application. 
}

or else try to use service of create a new intent of your alertbox activity:

 Intent intent = new Intent(Intent.ACTION_MAIN);
             intent.addCategory(Intent.CATEGORY_HOME);
             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(intent);
Aduait Pokhriyal
  • 1,529
  • 14
  • 30
Sethu
  • 430
  • 2
  • 13
-1

Write this code in your on backpressed override method

@Override 
public void onBackPressed() { 
   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_HOME);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);
}
grebulon
  • 7,697
  • 5
  • 42
  • 66
Raj Kumar
  • 688
  • 8
  • 18
-2

Just call

 System.exit();

in your catch block

Anas Reza
  • 646
  • 2
  • 9
  • 28