0

I have a app in which at Home Screen onBackPressed() i have used System.exit() but nither my app closes infact it gives me forceClose.

Home.Class

 @Override
    public void onBackPressed() {
        super.onBackPressed ();
        System.exit (0);
    }  
Anuj
  • 129
  • 2
  • 14
  • Let the Android system handle the app close for you? In the meantime, you can update your LogCat here. – Skynet Jun 27 '14 at 09:33
  • @ByzantineFailure Bad idea if the app is supposed to be secure- I want my banking app to actually close, not pretend to and keep my login info in memory. – Gabe Sechan Jun 27 '14 at 09:35
  • Prolly then, it should be developed in the NDK. Or signed with Proguard. Just clear the credentials. Its highly recommended by the Android framework not to kill the app manually. [To Back My Comment](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – Skynet Jun 27 '14 at 09:36
  • whats NDK and whats Proguard – Anuj Jun 27 '14 at 09:38
  • Sorry Anuj, that is not related to your original Question. Its a side note to my fellow friend Gabe. – Skynet Jun 27 '14 at 09:38
  • I rekon Gabe, but Google has provided ample of ways to overcome what feels overkill. I second what Google intends out of not force killing apps. However its a personal call, to jump across the fence or to play inside the lines. – Skynet Jun 27 '14 at 09:40
  • @ByzantineFailure Then do me a favor and please never write an app that deals with personal information or security- you aren't qualified for it. There is no reason for that type of app to stay open, and doing so only opens up holes for security breaches. Whoever thought that was a good global recommendation at Google should be fired for incompetence. – Gabe Sechan Jun 27 '14 at 09:41
  • Nah my fellow friend, the NDK is the answer to your insecurities. Also closing sessions, providing proper log out mechanism. etc, whatever has to be done has to be done within rules. Also if its so much security critical. – Skynet Jun 27 '14 at 09:42
  • There's no reason that you should have to write an NDK app in order to be secure- that's pure ridiculousness. Nor is it a solution, as an NDK app follows the same rules as a Java app. Providing a log out and closing sessions are good things, but simply closing the app to log out as well is far more reasonable, and how it works on every PC app ever written. Both should be provided. Besides which, a logout button doesn't help if the app was still open and thus the attacker just enters it from recents – Gabe Sechan Jun 27 '14 at 09:45
  • Its not good practise IMHO that instead of properly releasing resources and log out sessions you have to force kill that app -- that would be a shortcut. Why not provide a popup to log out the user when he navigates back to the extreme top logical parent activity? Also if one so critical of security, he should not let his phone slip out of his pocket [Here I back my comment](http://www.80vul.com/android/data-clone.txt). – Skynet Jun 27 '14 at 09:47
  • Why should I as the user have to spend extra actions to exit an app when I haven't had to do that in 30 years of using computers? Leaving an app should exit it. Hell, over 50% of Android users think that it does- including many of the programmers. I've had dozens of fights with people who think the back button does kill the program. And no, it isn't a short cut, it's insurance. If the app exits, I know the programmer couldn't have made a mistake. If the app stays resident, he may have. Why not eliminate the potential attack vector completely? – Gabe Sechan Jun 27 '14 at 09:53
  • And yet the slip out of the pocket happens- I lost my last phone when it fell into a cab I was leaving in Canada. There's also the times you hand it to a friend who looks at something, and he turns out not to be such a nice guy. Or you forget and leave it at your desk when you go to the bathroom. Everyone makes mistakes- that's why security has layers. When one fails the next one takes over. – Gabe Sechan Jun 27 '14 at 09:57
  • So you mean you do not logout from your Net Banking account, you simply remember the password and close the browser? :D Exactly that is my point, security has layers, killing an app would be the last, if I am to program :) I use an editor/ an IDE and a browser on my Linux machine, nothing is killed unless I kill it. There are proper mechanisms to log out of sessions. Maybe its a personal choice of how to program :) – Skynet Jun 27 '14 at 09:57
  • 1
    That's great- that doesn't mean it shouldn't be part of the equation. And if I have a choice between logging out or killing the app, I'll always pick killing the app- I don't know if the logout works 100% correctly, but I do know that killing the app does. Especially with native apps, where keeping the account info in a static variable is a common pattern. I want that static cleared. – Gabe Sechan Jun 27 '14 at 10:01
  • also in between loggin off, if someone hacks then?? – Anuj Jun 27 '14 at 10:02
  • There is something called as sessions, that is the answer to properly identifying your user. Also the Android system provides the lifecycle methods, in which you can determine if the app is in the foreground or background -- Its only a matter of Few lines of code then, to log the user out! – Skynet Jun 27 '14 at 10:06
  • This requires more emphasis on application design, focusing on business goals and not sticking with an implementation model tied to a previous application environment. Developers who lack the time or inclination to do this will get frustrated with newer environments that break their existing mental model. This is not the fault of either environment, any more than it is the fault of a mountain for storms flowing around it rather than through it. – Skynet Jun 27 '14 at 10:09
  • 1
    Also Anuj [Here is a better explaination](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2632649#2632649) to your Original question and why an app should not be killed. – Skynet Jun 27 '14 at 10:30

5 Answers5

0

Use finish() method instead of System.exit (0);

Giridharan
  • 4,402
  • 5
  • 27
  • 30
0

Use finish() instead of System.exit(0).

Mark Buikema
  • 2,483
  • 30
  • 53
0

System.exit(0) or finish() will not close the app it will exit from the current activity and will take you to the last activity.

If you want to exit from app try this may be it help

android.os.process.killProcess(android.os.process.myPid()); 

for more information look over this link

Community
  • 1
  • 1
nawaab saab
  • 1,892
  • 2
  • 20
  • 36
0

It suggested that you finish the current activity after you start a new activity

For example :

//MainActivity
{
//somecode there

startActivity(new Intent(MainActivity.this,SecondActivity.class)); //start a new activity
finish();// finish the activity so that when you press the backbutton it exits the app.
}
Rohit Goyal
  • 550
  • 8
  • 9
  • @Override public void onBackPressed() { SearchJobs.SearchJobsAsync async = new SearchJobs.SearchJobsAsync (c, true); async.execute (); SearchJobsCustomList jobsCustomList = new SearchJobsCustomList (c, SearchJobs.arraylist); } i did put finish there but it gives me error bcos of asynctask – Anuj Jun 27 '14 at 10:01
0

try this code

 @Override
public void onBackPressed() {
    super.onBackPressed();
      if (backpressvalue == 0) {
      Intent intent = new Intent(ControlScreen.this, MyCameras.class);
      finish();
      }
}
Irfan Mahar
  • 37
  • 1
  • 6