I am trying to show a Toast message from a class which is a customExceptionHandler. But I am unable to do that.
I have seen the similar issue like Displaying a Toast message from the Application class But still, it didn't solve my issue. Is anything I'm missing here.
I can see the Log statement in Logcat but the toast is not showing up
public class MyApplication extends Application {
private Thread.UncaughtExceptionHandler defaultUEH;
private Thread.UncaughtExceptionHandler unCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, final Throwable ex) {
Log.e("Inside Run", "******************** Inside uncaughtException ***************" + ex.getMessage());
Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT).show();
}
};
public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(unCaughtExceptionHandler);
}
}
I have tried one more thing that, I know static methods and variables are not an optimal solution, but I have created a static method in a util class maintained activitiy context in a static variable and used that to show a toast that one also didn't work
Like
Util.showToast(ex.getMessage());
in the uncaughtException() method
If you want to show Toast in your Application calss than it must have a context to show Toast so you can pass Context in your constructor or get instance of activity
I have not try this code but you should try this: