Word up, I need to update a context menu for a widget (listview in this case).
The items for the menu need to come from a call to a web service. If the web service call is made synchronously on the main UI thread then this works.
However due to fact that I'm calling a web service it needs to been done asynchronously via an AsyncTask or similar to avoid ANRs etc. This asynchronous update of the menu via menu.add() within the onCreateContextMenu doesn't work, i.e. asynchronous calls to menu.add() don't result in the context menu being displayed. Also note the async menu updates are done on the UI thread via the onPostExecute of an AsyncTask.
All Ui updates have to be done on UI thread. So you will have to post a runnable onto Ui thread.
Something like
runOnUiThread(new Runnable() {
public void run() {
//DO UI update here
}
});
Or you will have to call publishprogress() in doinBackground function of Asynctask and then do Ui work on onProgressUpdate().
If the Ui update can wait you should do it in OnPostExecute
Thanks to those that have provided answers, I may have to leave this for now with a somewhat half baked solution. For those reading this that are experiencing the same problem, my current solution uses the openContextMenu(View view) method in the activity to call back to the onCreateContextMenu. A simple boolean state flag determines whether to do the async call or populate the menu with the data from a previous async call. This design feels a bit awkward and fragile to me but works.
Save the information, you get from the web-service in the activity. Then in onPrepareContextMenu() use this information to update the menu. onPrepareContextMenu() is called before the context menu is displayed. OnCreateContentMenu() is only called once for every activity instance.
Related
I am working on an app where i do some calls in synchronized manner using the
class HttpUtil extends AsyncTask<Void,Void,String>
{...}
HttpUtil httpUtil = new HttpUtil();
httpUtil.execute((Void[]) null).get();
This will call to a AsyncTask method.
Issue:
The spinning wheel is not shown in the UI since we do a UI blocked request. Even if I add a toast then the toast is displayed after the request is completed.
If I make asynchronous calls then I get the spinning wheel as the UI was not blocked
Expected:
I need to show a spinning wheel for the blocked request(done adding get() method) also.
Do not use get() on a network operation. Just don't. Your app will freeze, and either get killed by the system, or the user will be frustrated that your app blocks the entire UI of the system. It's absolutely unacceptable, and there is no reason why a well-designed app should need to resort to that.
You turn on your indeterminate progress in the AsyncTask's onPreExecute(), and turn it off in onPostExecute(). These two methods are always run on the UI threat. Please refer to the documentation for AsyncTask.
Further, you won't need to pass Void[] null to the execute() call -- just pass nothing, which will result in an empty array of Void.
If any other operations or UI updates depend on the result of the request, then do those updates in onPostExecute. If you want to create modality to essentially "halt" the UI while the request is running, then display a dialog box, but please provide a cancel option.
I have a activity with a Listview and a adapter attached to it. I have a class which syncs data and hold it. (I think we should not care about from where data is coming) and it runs on a different thread. Now I want to know the clean way to update adapter but We should not call any function of activity from that class as it runs on different thread.
One way I know is to create handler in activity and pass it to other class and use it from there. But I want to know if activity is in background then activity's function can be called by UI thread or only when activity come in foreground.
I do not want to miss any update and want to update activity when it is in foreground.
If you want to update your listView from another class you can have a few ways to do that.
Send broadcastIntent from your worker class and add BroadcastReceiver to your activity and when you receive the right message, update your listview.
Second way is to create private or public class in your Activity which extends AsyncTask and in your doInBackground() do your work and in onPostExecute(result) update your listview.
Third way which I can imagine, but I don't think it's the best way create a static method is your activity which you can use from any other class for updatiogn your UI.
The best thing which you can use here at least in my opinion is AsyncTask.
this code will do what you want:
runOnUiThread(new Runnable() {
#Override
public void run() {
//your actions
}
});
We should not call any function of activity from that class as it runs on different thread.
That's wrong. (in java an object is not running in a thread. What you can say is that a method is running in the thread from which the method was called)
In Androïd (and in most UI frameworks) the rule is this:
You can only call a method updating UI from the UI thread.
If you have some code running on a thread (not the ui thread) and if that code need to update the UI : you can use the Handler of the UI-thread to post UI update code to the UI-thread. If the activity is not in the foreground when you post something to update it's UI : nevermind! the code you just post will be executed at some point in the future.
You should use AsyncTask, take a look at http://developer.android.com/reference/android/os/AsyncTask.html
Why can an AsyncTask perform only one job? For example,
task = new SubAsyncTask(...); // assume the parameter is correct.
task.execute(...) //
task.execute(...)// calling once again, it throws exeception.
But a Handler can continously perform more than one task:
hd = new Handler(...); // assume the parameter is correct
hd.sendMessage(...); //
hd.sendMessage(...);// no exeception is thrown.
Is an AasyncTask object for a one-time job only? If I don't want to create multiple object for similar task, should I choose Handler?
Handler and AsyncTasks are way to implement multithreading with UI/Event Thread.
Handler allows to add messages to the thread which creates it and It also enables you to schedule some runnable to execute at some time in future.
Async task enables you to implement MultiThreading without get Hands dirty into threads. Async Task provides some methods which need to be defined to get your code works. in onPreExecute you can define code, which need to be executed before background processing starts. doInBackground have code which needs to be executed in background, in doInBackground we can send results to multiple times to event thread by publishProgress() method, to notify background processing has been completed we can return results simply. onProgressUpdate() method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update event thread, onPostExecute() method handles results returned by doInBackground method.
So, you dont need to call execute method on AsyncTask multiple TImes, instead you can invoke publishProgress.
Because that is how the class was designed. The idea is: do something with UI (show progress dialog, etc.), do work on background thread and return results, update UI. The Handler is fundamentally different: it lets you post messages, but it does not create a background thread for you. If you don't like how AsyncTask works, build something similar by using threads/executors and handlers.
Currently, my app starts with a menu. Once a menu item is clicked, it starts a Activity which grabs content from an online API.
The problem I am having is, once the menu item is clicked, the application waits until the http request and response is complete before it displays the activity.
My Question is, How can I have the Activity display instantly with the layout and static View. Then once the activity is loaded, then I start fetching data from an API?
Why run after when you can run concurrently!!
Use an AsyncTask in your onCreate()/onResume() method.
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(...);
AsyncTask task = new MyTask();
task.execute(/* optional params */);
}
In your implementation of MyTask, put your long running code (the data fetched from the http request) into the doInBackground() method. Once the doInBackground() method returns, you can update your View in onPostExecute().
Also, take a look at Painless Threading, as there is lots of helpful android threading information in there.
You may use either an AsyncTask during the onCreate method to fetch your results and update your activity Asynchronously. Additionally, I think you may use a separate thread to do the same functionality. Pretty common use case. Here's the relevant Android docs.
I have a a service class which includes an Async task. In the doInBackground and onPostExecute I call some methods which are in the service class but outside the Async task. When these methods get called will they still be in the thread created by the Async task and therefore not interfering with the main UI.
To illustrate my lack of understanding a bit more should I try to get almost everything that the service class does into the Async task. For example the service starts up as the result of an alarm and in the onStartCommand sets a repeating alarm (this is as Reto Meire's Earthquake example)and creates a database. Would it make sense to move the code for these two operations into the onPreExecute part of the Async task?
No need to do that.. make sure that the method which you want to run in background is called from doInBavkground().. rest you can put in postExecute.... the snippet which you want to run after the backGround task should be in PostExecute(). If You call methods from doInBackground() they still run on background thread.. does't matter where they are.. and if you call it from onPostExecute() then it will run on other thread which ofcourse can make changes in your display.. just like showing dialog etc...
You should always offload as much work as possible to background threads/tasks. Something like database creation should be done in the doInBackground method. The onPreExecute and onPostExecute methods run on the UI thread and are generally used to inform the user of background activity (e.g. using a ProgressDialog).
From experience, I also suggest not using a background service if you can get away with it. If you use one, you should know how to clean it up properly since users generally get annoyed with an application running in the background.