getting data from dialog window to main thread - android

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

Related

How to send a message Activity to Thread class

This is my problem. In my android application I have a process which is execute by the Thread class. During this process I need to send a message to the user to get confirm he needs to do it further. In my Thread class I have only Service instance and don't have an activity instance. So I used separate dialog activity to display a AlertDialog to the user. Now I need to get the answer that the user chose in this dialog activity box.
I know we can use Messages and Handlers for sending messages between thread class to Activity. But my problem is different than that.
Is there any way to get the user answer to thread class or else is there any way to display a message box to user inside the thread class rather that using dialogActivity ?
Do you have any other suggestion to solve this issue? Any clue or help would be appreciated.

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

Custom Dialog for Force Closed 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.

ProgressBar Dialog not showing

I am new to Android development and am facing a slight issue. My first screen is a basic log in screen. There is a LOG IN button on the screen and a OnClickHandler implemented for the button.
When the user clicks the log in button i validate the user name and password, shows a ProgressBar dialog start a new Thread. The thread connects to the server and validates the user info. The problem is that the Progress Bar doesn't show until after the thread has finished. I read that all work must be done in a separate thread otherwise the Progress Bar will not see the light of day and i am doing my work in a separate thread.
But the problem is that right below the thread code i have a loop while(userinfo==null){} . This is because the userinfo object is been populated by the newly created thread and the userinfo object is required by code below the thread, without the loop a new thread would be created that fills the userinfo object but in the mean time the code that reads the userinfo would get a null object. If any one is willing to review the code i could send the file that has this code. Really need some help in this.
You might find AsyncTask helpful. Do whatever it is you do in the loop in postexecute instead.

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.

Categories

Resources