ProgressBar Dialog not showing - android

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.

Related

on Which Activity background I should display progress dialog when user can navigate to different activity based on his data in server

In my app I am creating Log in mechanism.In this when user enter username and password then in background data is fetch.On the basis on this fetched data user can navigate to different activity.Like if user is registered,if user not registered,if username and password is incorrect.
I have implememnt all this functionality.But here I am displaying progress dialog on the login Activity and then I navigate the user based on condition satisfied.But as the data is large hence activity layout taking more time to load and I have already dismiss the dialog in onPostMethod of AsyncTask.
So for 2-3 sec login activity is standstill without dialog(as it is already dismiss)
Hence where should I display the log in progress dialog.
Please tell me if any other detail is needed.
I would take the route of checking whether or not you will still be using the Dialog UI component based upon the response. And if it involves a task that will take some time to accomplish, I wouldn't dismiss the ProgressDialog at that moment.
I would wait until the operation completes in your follow up Activity and then dismiss it there. Not sure what else we can as a community offer as a solution without any code or direct issues. We can probably just speculate and come up with architectural/structural responses.
Process all data in doInBackground in onPostExecute just navigate to new activity. for checking is user registered or not etc use some flag in doInBackground method.
Dismiss the progressDialog immediatly ofter your startActivity() call.
*Make sure you dismiss it in the UI Thread.
I'm working in mono for android c# and my code will look like this (just change it to java):
var myIntent = new Intent(this, typeof (MyActivity));
StartActivity(myIntent);
_progressDialog.Dismiss();

Is using runOnUiThread inside AsyncTask inefficient and bad?

I know it sounds crazy that someone is using runOnUiThread inside AsyncTask. Somehow, it is working for me but I wanna know if it is an acceptable and robust approach or not. Here is the scenario:
I have an app in which after successful login, user is rendered to next screen. In this new screen, 3 different methods are loading different types of data from a web server. These methods are:
getMembersForList() : It loads the list of community members and shows it in a listview.
getProfileData() : It loads the profile of logged in user and shows his name , image etc on the screen.
getNotificationCounts : It loads the count of new notifications for the user.
I applied 3 different approaches for it :
(1) Calling all 3 methods simply in onCreate i.e. no exclusive thread is being used for any of the methods . In this case , the transition from login screen to this screen becomes very slow and black screen shows up for some time before this activity shows up.
(2) Calling getMembersForList() on UI thread and the other 2 methods on exclusive threads. In this case transition becomes fast and list shows up quickly but Notification counts and username etc. don't show up because WrongThreadException occurs saying that this thread can't touch other thread's views (TextViews for username, notification count etc. which are declared globally) . The same thing happens when I start these threads from an AsyncTask as well.
(3) Calling getMembersForList() on UI thread and then starting an AsyncTask in which the other 2 methods are being called in "runOnUiThread" inside doInBackground() method. This solves both the above issues. Now the screen transition is faster and the WrongThread exception is also not occuring.
So far the approach-(3) is working good for me but I am not sure if this is the right way to do it because runOnUiThread and AsyncTask are 2 completely opposite things. Can anyone please clear my doubts about this scenario. Thanx in advance.
Yes, use-cases like this are a big reason why the runOnUiThread() method exists in the first place. The idea is you allow your background thread(s)/AsyncTask instance(s) to run your lengthy operations in the background, and then provide a simple hook that they can use to update the interface when they have the result (or at arbitrary intervals, as different pieces of the result become available).
As long as that's what you're doing, then your usage is fine. What you want to avoid doing is performing a lengthy operation on the main thread, either directly or indirectly by passing in some lengthy operation from a background thread.
Of course you don't have to do it that way if you don't want to. You could use postExecute() instead. Or you could store the result somewhere and then use any sort of message-passing API to notify the main thread that the result is ready, and so on.
I would advice to run all the 3 calls in the asyncTask, and update the UI in the postExecute() of the AsyncTask after the background taks is complete, postExecute runs on UIthread so you need not call anything explicit to run them on UIthread.

Android application being too laggy

My Android application is running very slow and lagging much. I have PHP API on my server and my application requests data through HTTP.
Though, the problem is that sometimes I should wait for few seconds before I can see the result. I have all calculations done in the main thread in onCreate (parsing XML, adding controls) and downloading data from HTTP server in AsyncTask.
How to optimize my program to make it faster? I want it to load activity first and only then, in background, download and parse data. How is it possible? Sorry for newbieship.
what did you mean by lagging ? Will you elaborate more on the issue.
One suggestion that remove parsing XML from OnCreate and move it to AsysnTask. The reason for this is as you are doing time consuming operation in UI Main thread which will impact the activity to be shown.
Create thread to perform HTTP related operations and parse the response on the same thread and while doing the parsing operation show dialog.
Dismiss the dialog when parsing got completed and then show the activity which you want to display.
In the doInBackground() method in AsyncTask add the data parsing and create the data objects then in onPostExecute update the ui elements.
The reason to do something like that is for the application to be responsive in all this time and just make small jobs on the ui thread so as not to freeze.
You can instatiate your views to a default state an add a progress somewhere on top to indicate that the activity is currently loading. For example you can create an empty ListView or a Button that cannot be selected and when the parsing is done then you should set the adapter to the list and make the button back to selectable again.
All this things can be implemented according to what you want the user to be able to do in the time of his waiting.

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

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