Refresh ActionBar from an AsyncTask - android

I have a ListView Activity that uses a AsyncTask to load data from the database. This Activity has an options menu that checks to see if there are any data items in the Activities ListView in onPrepareOptionsMenu().
If there are items, I enable one of the options that is shown on the ActionBar that allows the user to delete the items.
Now, when the Activity starts, the AsyncTask starts and as onPrepareOptionsMenu() is run through while the AsyncTask is still running, this menu item is never enabled, unless the device is flipped and the listview data gets passed in as an instance, bypassing the AsyncTask.
So, in the AsyncTask's onPostExecute(), I call invalidateOptionsMenu() but that does not seem to be the menu to refresh (I have debugging code in onCreateOptionsMenu() and onPrepareOptionsMenu(), and neither is fired). Any help appreciated.

You can try creating a global boolean like haveData on your Activity and upon onPostExecute(), set the boolean to true or false accordingly. Then based on the boolean do a check on your onPrepareOptionsMenu() and enable the menu item accordingly.
The last time i tried it worked for me not sure why it didn't for you. Do a debug from there if it doesn't work tell us where it isn't executing.

The issue was related to a bad global variable value. I was gatewaying the code inside the onPrepareOptionsMenu() method with a global boolean value that was never true, and had put the debugging statement INSIDE the boolean check... resulting in the debugging statement never running.
After addressing that issue, everything is working as expected, code above is correct.

Related

Handling intense Swipetorefresh function in Android

i want to know is there any way to know the state of swipetorefresh() function in android. what i mean is whenever i called swipetorefresh() to refresh a listView intensely (calling it when the function is still running over and over again) it executes it, despite that it is still running, thus make my app crash. when i log it the problem is index confusion in my adapter so if there is a way to know that the swipetorefresh() is still running i can reject the user calling method swipetorefresh() again and again.
You can use Boolean to check if swipetorefresh is running or not
Just set boolean value true in onRefresh() method and set it false when you get response from your API call.
And when boolean is true disable swiperefresh.

Android - Create dynamically a menu after a server response

I would fill dynamically my menu item after a request at web server.
I have created my AsyncTask class, but when start my activity, rightly I obtain a
NullPointerException in onCreateOptionsMenu.
It's possible create the menu after that AsyncTask finishes the process?
When your AsyncTask returns the result in the onPostExecute, call your Activiy's invalidateOptionsMenu method:
http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()
That will cause the onCreateOptionsMenu to be called.
The issue now is to inflate MenuItems from your AsyncTask result (and stick them into the Menu):
http://developer.android.com/reference/android/view/Menu.html

Android:ListView not displaying items when power button locks the screen during AsyncTask

I have a Async Task that creates a HashMap to create a Adapter to populate ListView. I have a progress dialog that shows during doInBackground method.In onPostExecute() method, I dismiss the progress dialog and call a method that populates my listview with the list of items saved in doInBackground method.
This works fine. But I noticed something strange:
The issue I see is, if I lock the screen when the progress dialog is about to be dismissed (in onPostExecute), the listview does not display, even though it has non-empty items in it. I verified it in logcat messages and when I debugged.
Is there a possibility that a screen lock (I do this my pressing power button once) blocking UI thread? How can I resolve the issue and make sure ListView displays its items?
Code for onResume():
#Override
protected void onResume(){
super.onResume();
if(MyAdapter !=null){
pull_listView.setAdapter(MyAdapter);//pull_listView is listview
MyAdapter.notifyDataSetChanged();//MyAdapter is the adapter
}
}
This situation illustrates the downside of using AsyncTask to do background processing. In some cases, an IntentService may be a better choice, especially if you think the background work is going to take some time. An AsyncTask ties the background work to the current Activity, while an IntentService is completely decoupled.
The Android training class Running in a Background Service shows you how to set up an IntentService, request work, and notify your Activity when the work is done. Passing data from the IntentService to the Activity is a bit more complicated, but there are options.
onStop() will be called when your screen goes out as per the Activity Lifecycle. You could override onStart() or onResume() and put a check in there to see if your data has been populated. If not, populate. You may even want to overide 'onStop()' to save data if the screen goes out.
Edit
In this particular situation, I would think onResume() or onStart() would be fine but onResume() is usually the safest because it is guaranteed to get called before the Activity is shown as illistrated in the link I gave. What kiind of a check you want to use is up to you and depends on how you handle evrything. However, if your AsyncTask is an inner class of your Activity class the you could simply create a boolean member variable, say boolean isDone=false; change this to true in your doInBackground() or onPostExecute() then your onResume() knows the data Is loaded. If its false then you can try to get data again. Hope this makes sense and can help

Show ProgressDialog while loading AppState from file

When my activity is being closed, I serialize the application state data to file to give me a chance to reload the state if the app gets killed by the system.
This approach (saving and restoring state) works fine. But, sometimes, when the process was killed, and depending on the amount of data to load, the loading state process can least some seconds to complete.
So, I can't load the state on a separeted thread because my activity will crash if the data isn't there on the onLoad.
So, I'd like to display a progress dialogue while loading the content, but, ensure that the Activity's onLoad method will be called only after the loading state process.
Does anybody knows how could I achieve this? Thanks a lot.
Hmmm.. could you Override the onLoad() function, then add a boolean that states whether you're displaying the dialog or not. If the dialog is being displayed (the boolean is true), don't let onLoad() do anything; otherwise, carry on as usual. That way once the dialog is dismissed, you can set the boolean to false again, and if onLoad() isn't called by default afterwards, perhaps you could manually call it? This is just an idea.
In other words: the dialog can be displayed using an AsyncTask. In the onPreExecute() method, set the boolean to true, do everything you need in doInBackground(), and in onPostExecute(), set the boolean to false and call onLoad().
On a side note... do you mean onCreate() or onResume()? I've actually not heard of onLoad(), unless this is a function you created.

Callback when the options menu creation is complete

I need to make sure that the options menu has been created before i run a certain code that accesses one of the menu items. Is there a callback for that or how can i implement one?
Is there any guaranties that the menu has been created on the activity's onResume()?
You cannot use onResume for this.
In fact this is the call order when your app is launched :
onResume()
onCreateOptionMenu
onPrepareOptionsMenu
As you can see onCreateOptionsMenu is called AFTER onResume
(you can verify this yourself by writing to the console in the overriden methods).
Also notice that onPrepareOptionsMenu is called at least once - when your app is starting, even though the menu is at that time not shown to the user.
Now, you do not write in great detail about what you are trying to do, but if what you want to do is this "run a certain code that accesses one of the menu items", then you can use onPrepareOptionsMenu as it is called AFTER onCreateOptionsMenu and it also called whenever your app comes to the foreground - i.e. after onResume. Depending on whether this code needs to run once or everytime you can use a boolean flag inside that method (or store it in the Preferences or similiar persistent data if it is only once ever).
There are no other callback hooks mentioned in the documentation and I have never had the need for others, as onPrepareOptionsMenu should be enough to do the job. If you feel this is not the case, you need to be more specific in your answer and provide code for your specific usecase.
But as I said before, no other callbacks are mentioned in the documentation.
It is not a callback for after onCreateOptionsMenu is done, but onPrepareOptionsMenu can be used if you want to modify the menu before it is displayed to make modifications. This would only be called after onCreateOptionsMenu (if Android is behaving, which is should).
http://developer.android.com/reference/android/app/Activity.html#onPrepareOptionsMenu(android.view.Menu)
See the Activity documentation on this:
http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)

Categories

Resources