Custom Dialog for Force Closed Android - android

I want my Application to show Custom Dialog When an Uncatch Exception occur.
For that reason, I am implementing Thread.setDefaultUncaughtExceptionHandler.
I am successfully able to handle all my uncatch Exceptions.
And Try to show the Toast.
But My Toast is not Displayed because my Main Looper (i.e My Main Thread) is being stopped and My Whole Application get blocked.
I tried to create a Alternate Main Looper when Main Looper is being stopped. But for some reason my Alternate Main Looper does not get Any Message in MessageQueue.
Is there any other way to do that.
What I need is to Show the Error to User and Close the Erroneous Activity, Not my Whole Application.
Or Else is there any way that I can Handle all the uncatch Exception in an Activity.

I would think if the error reached this point your application would be kill (thus no context to display your dialog) - I'm not certain on that however.
You could try catching the error yourself, then displaying your dialog and then throwing the UncaughtExceptionHandler exception on the dialog's closing.

Related

My app's UI hanged

I am writing an android app, in which I have 1 button and 1 progress bar as UI elements.
The main aim of this app is when user presses this button, it has to create a database which contains all phone book contacts in customized format, means I am reading Contacts database and manipulating for my requirement.
So I am using SQLiteOpenHelper for database operations. I written a method downloadPhonebook() to perform all required operations. I written app such that when user presses button I am making progress bar visible and calling this method.
In this case, UI was hanged after clicking button and showing a dialog with Force Close and Wait buttons, after 15 seconds.
To avoid this I tried following mechanisms.
-> Broadcast Button click message and call method downloadPhonebook(). Here no use, same problem occurred.
-> Used a Thread and AsyncTask to call this method, here I got Runtime exceptions like Couldn't create Handler inside a Thread, Looper.prepare not called. I tried calling Looper.prepare() and Looper.loop() even exceptions occurred.
-> I tried with Android Service and Broadcast intent, again same problem UI hanged.
If anybody faced this problem or knows the solution or knows how to use Looper.prepare and Looper.loop please reply me. Thanks.
the workflow should be something like this: create a handler in your main class, add a handler in your sql helper class, pass the handler from the main class to the sql helper class when you create it. Run the download on separate thread from your main class, when download is ready, call yourHandler.sendEmptyMessage(0). You should override the Handler.handleMessage (I'm not sure about the exact name of the method) in your main class. You can also send messages to update the progress, read about Andoid Handler for more information

getting data from dialog window to main thread

Okay, I am using pro android 3 book as reference and implemented modal dialog box exactly the way stated in the book. However, I am having hard time getting string from textedit entered in the dialog box to main (calling) thread which calls the dialog box.
FIrst, book tells to implement independent class for implementing the android.content.DialogInterface.OnClickListener. In this case the text will not received by main thread as process is asynchronous which is what book tells.
And it tells that the solution is the calling activity will directly implement the callback in android.content.DialogInterface.OnClickListener which will solve the issue of premature update of string in the main thread. That is main thread will receive the return string only after dialog box is closed.
So I re-architected my application but still it is not working. The main thread is returning the string as 'NULL immediately before the submit button is clicked. Can anyone explain what else I have to do? Thanks

How to display Custom message before force close or ANR message will occur

How i can display custom error message before foreclose or Application not responding message will happen for an application.
Or
Can i display Custom message like "Please wait....." instead of Application not responding message.
Thank you
You have to make sure you are handling any exceptions that are thrown. That is what causes a force close, an uncaught exception. as for the "not responding" message, again, this is up to the developer. You need to spawn new threads and do async tasks, that way processing does not block the main thread, which is what the UI runs on. When the main thread is blocked, that is when the "not responding" message happens. If you spawn a new thread, then you can display what ever kind of message you want to the user to notify them that something is processing.
I'll add to Ryan's answer that you can handle all critical exceptions without the need to wrap most of your code in try-catch. There is a beautiful method for that: Thread.setDefaultUncaughtExceptionHandler. It is mainly useful for bug reporting. For the purpose of showing a message you have to set the handler in Application.onCreate (you'll have to set <application>'s android:name in the manifest to your custom Application class), save the application context and then use it to show a notification in the status bar - it remains after your application has been force closed. Also, in order to have the Force Close dialog appear instead of just freezing after your exception handler has worked, you should call defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); in the handler's constructor and then in the end of public void uncaughtException(...) call defaultHandler.uncaughtException(thread, ex);.

Loading an Alert Box from a Thread when the activity has gone away

I have a comment activity that loads a Thread and sends some data to a server; the activity is immediately finished once the submit button is pressed.
The user is then free to do other things in my application.
When the server responds an AlertDialog is shown.
The problem is that since the initial context has been destroyed, my application crashes.
I tried getApplicationContext() but still get an exception.
Put your network stuff in a Service, then show a status bar notification instead of a dialog.
Take a look at AsyncTask
From JavaDocs:
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

nested AlertDialogs in android

I'm trying to display an AlertDialog inside of another AlertDialog. When the user clicks on the any item within the initial AlertDialog another AlertDialog is created and shown.
I'm following the correct pattern for creating and displaying AlertDialogs, the problem is as soon as the code reaches the point where the innerDialog.show() method is encountered the application fails. The logcat prints an uncaught runtime exception:
android.view.WindowManager$BadTokenException : Unable to add window -- token null is not for an applicaion
I'm wondering if i'm allowed to call the show() method on the innerAlertDialog manually.
The outer AlertDialog is working because i'm using the callback onCreateDialog() method.
In previous versions there was a bug where getApplicationContext() returned null.
I still dont quite if it has been fixed however with dialogs, its always better to send this.
And for the main question, its working as designed to avoid locking the UI thread.
I have seen people recommending creating a new layout with the theme of the dialog and start the activity on the first dialog, however I havent try this yet.

Categories

Resources