0

I did some search and found people who ended up with the same error as I did, but in a completely different context :(

So, I'm trying to get my app to gracefully exit when something critical happens. It's an android app that uses camera, and I want the app to quit after showing a toast text, so the users don't get a random crash. (And yes, theoretically I shouldn't end up in the if statement because I require a camera hardware in my manifest, but I like handling error as much as possible)

Here's a snippet of the manifest file:

<uses-permission android:name="android.permission.CAMERA" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

And here's my code:

public class MainActivity extends Activity {
/* member variables... */

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Create an instance of Camera
    // I forced this to true for testing because the devices I'm testing
    // against won't reach inside the if statement
    if (/*!(checkCameraHardware(this) || checkFrontFacingCameraHardware(this))*/ true)  {
        Toast.makeText(getBaseContext(), "Shutting Down! No Camera Detected!", Toast.LENGTH_SHORT).show();
        System.exit(0); // This is the last code it reaches
    }
    mCamera = getCameraInstance(); // This code isn't reached

As you can see, I would like the code to make a toast, then shut down, but it gets stuck in a blank (black) screen instead (at or right after System.exit(0)), repeating the following error in Log Cat instead.

Tag: Trace, Text: error opening trace file: No such file or directory (2)

Did I miss something? Can I not exit or toast in the OnCreate() function?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
tony
  • 88
  • 8
  • 3
    `System.exit(0); ` bad idea – Raghunandan May 04 '14 at 18:29
  • Don't exit, instead go to a dead-end activity that displays an informative error message and nothing more - if you really want an out, provide a button to fire a home intent, or even an uninstall one. Most likely this should be closed as either unclear, or a duplicate of one of the many existing questions on the irrelevance of the titular error message. – Chris Stratton May 04 '14 at 18:36
  • Thanks! I replaced System.exit(0) with the code from this answer http://stackoverflow.com/a/7239827/1357261 and I still get a those error – tony May 04 '14 at 18:37
  • 2
    You should not be trying to exit your process **by any method at all**. And your error message is irrelevant. Don't exit, inform the user then give them the chance to go to home or the uninstaller instead. – Chris Stratton May 04 '14 at 18:39
  • @tony what is that you want. there is no quit for android. Each activity has a lifecyle and you need to handle it. Activities are added to back stack and popped. – Raghunandan May 04 '14 at 18:39
  • Did you try getApplicationContext() instead of getBaseContext() – Typo May 04 '14 at 18:42
  • possible duplicate of [Quitting an application - is that frowned upon?](http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon) – Raghunandan May 04 '14 at 18:43
  • Thank you for the comment, guys. I'm new to android programming and this has been helpful. perhaps the problem wasn't that I'm using a certain kind of quit, but that I'm quitting at all. I'll change my question in such a way that doesn't try to exit the app. Thanks for the insights! P.S. I'll be deleting the question as the solution to my problem is completely different from my original question. Thanks! P.P.S... Sorry I'm new to StackOverflow, and I didn't realize I could delete a question with answers... – tony May 04 '14 at 18:48
  • @Benjamin - making *trivial* edits to already resolved, old, or abandoned questions only bumps them back to the top of the page earning them attention they neither need nor deserve. – Chris Stratton May 13 '14 at 19:24
  • @ChrisStratton I've edited quite a lot of Q/A here, and never noticed that it bumped the results to the top of the list. What makes you think that they're bumped to the top? – BenMorel May 13 '14 at 19:27
  • The **fact** that they are. Take a look at the main page. – Chris Stratton May 13 '14 at 19:28
  • I did. Several times. On several posts. Still can't find any of them there. – BenMorel May 13 '14 at 19:29

1 Answers1

1

Do not use System.exit(0)

Quoting Romain Guy from https://groups.google.com/forum/#!topic/android-developers/G_D3pKnGLt0

The user doesn't, the system handles this automatically. That's what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a "quit" or "exit" application. It is useless with Android's application model. This is also contrary to how core applications work.

There is detailed answer about the same @

Is quitting an application frowned upon?

You can also read related threads @

https://groups.google.com/forum/#!topic/android-developers/G_D3pKnGLt0

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    Thank you for your insight. I'm new to android programming, and I'm used to having a software quitting after notifying users. This post (along with the other comments to my question) has been very helpful. - Tony – tony May 04 '14 at 18:52