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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.