I've been wondering if it's possible to stop a crash in an Android App by capturing said crash in a parent activity.
Lets say I cause a Fatal Exception in the onCreate Method of a child activity, will I be able to capture that exception in anyway? Or will the app crash no matter what I try?
Here is an example of what i mean:
Main.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ly_main);
// My Main activity starts
try{
// Call the next activity
Intent intent = new Intent(getApplicationContext(), Child.class);
startActivity(intent);
}catch(Exception e){
Log.wtf("Exception_WTF","Exception from child activity woohoo \n "+ e.toString());
}
Child.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ly_child);
// Create exception... for science
int a = 0;
a = 1/a;
}
This does not work. The child activity dies and takes the parent with it.
Will it be possible to do it via startActivityForResult?
Thanks,
Edit: I don't need the crash data, i just want to know how can i avoid the app crashing.
Looking around i found: Using Global Exception Handling on android
which includes this piece:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
Log.e("Alert","Lets See if it Works !!!");
}
});
That Let me log the uncaughtException, avoiding the "Crash", nevertheless, the App went blackscreen and stopped responding...
Edit 2: After a lot of reading (thanks to user370305) in the thread How do I obtain crash-data from my Android application?
I've reached a dead end, either I handle the uncaughtException and call defaultUEH.uncaughtException(paramThread, paramThrowable); so the app Crashes, or i don't call defaultUEH.uncaughtException, the app doesn't crash, but doesn't respond either... Any ideas?
final Thread.UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
Log.e("Alert","Lets See if it Works !!!");
defaultUEH.uncaughtException(paramThread, paramThrowable);
});