Handling intense Swipetorefresh function in Android - 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.

Related

Android : How to detect if my Application class was opened by user click or by itself in background

I wanted to know when my Application class' onCreate is called, whether it was created due to user opening the app or by itself due to some reason like broadcast received/job started etc. Please help.
You can set boolean variable inside pref. Set this variable to true inside your broadcast/service.
And Inside onCreate method just check for that value. And also set it to false after using it in onCreate
Better approach would be to set a flag/tag from your service as described in below post. And in activity check is the flag/tag is available and perform necessary operations.
How to get data from service to activity

Is possible to check running application currently is showing or not on Android device

I'm working with Receiver and I can detect Wifi state changes in Wifi Setting System.
But I need to know specified application is showing or not to do more thing,
ex. if specified application was hide in Background, do nothing, and if it was showing for user to integrate, I will do something.
People who know how to check specified application was running is showing or not,
Please help me,
p/s :
I have 1 Receiver and 1 Activity (is "specified Application" as I mentioned)
I can integrate in onReceive method, it means I know how to receive signal.
Thanks.
SOLUTION
Okay, thank you,
I know how to check.
I need stored public static boolean in onPause method, to indicate current is invisible state.
And when onResume method was called, the above boolean will be in Visible state.
How to check if activity is in foreground or in visible background?
Okay, thank you,
I know how to check.
I need to store public static boolean in onPause() method, to indicate its currently in invisible state.
And when onResume method is called, the above boolean will be in Visible state.
How to check if activity is in foreground or in visible background?

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.

Refresh ActionBar from an AsyncTask

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.

Asynctask API call issue

I'm currently using an AsyncTask to make an API call and populate a list with data.
I have a Sub Menu whose items can call the AsyncTask to populate the data, problem is that if i click quickly i end up with merged results obviously cause the AsyncTask is running at the same time as each other.
What is the best way to handle a situation like this? Sorry if this is a amateur question.
I would use a ProgressDialog to show that content is being updated, and when the update is complete, dismiss() the dialog. While this is happening, you should make sure that you are not accepting touch input on your ListView. (this may happen by default when the ProgressDialog is in front, I am not remembering currently...)
Take a look at this link for an example.
You could configure only a single Asynch task to run at a time.
A boolean variable which is set to true as soon as the asynch task starts and set to false as soon as it finishes.
Next call could wait for this to be set to false. You could also rate limit what is the min time after which only making the API call makes sense.

Categories

Resources