AsyncTask updates data after fragment is shown - java

I have fragment that displays data about user downloaded from web service. I want to download that data after the fragment is shown, just like some applications do. While there's no data avaliable, I would like to use either some default value or ProgressBar. What is the proper way to do this? Should I call asyncTask in onCreate or onCreateView method? (since it's not clear if this fragment will be ever shown)

I would do it in the on create method. You want the dialog to begin in the doInBackground method of the Async task and hide the dialog in the on post execute method where you update your view depending on what information you get back.

Related

How to make sure that data that has been retrieved from a server is not re-retrieved every time a fragment is called

I am working with fragments and a navigation drawer. I have two fragments and a fragment manager to recycle them when I click on the menu items in the navigation drawer. I am also receiving data from a server in one of the fragments.
However, every time I reinitialize the fragment, the call to the server occurs again. I would like to know if there is any way that I can make sure that the server call only occurs in the beginning and when I click the refresh button.
I would like to know if there is any way that I can make sure that the
server call only occurs in the beginning and when I click the refresh
button.
Yes , when you click on the refresh button use an ClickListener to trigger the call.
If you want the call to occur only when fragment is created you can refer to the fragment life cycle. The better way is to put this call in the onCreate() method.
However if you don't want to make a call each time the fragment is created i recommend you tu use the SharedPreference to save the information that call have already been made.
exemple:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
// The seconde value of method getBoolean incates the default value if constant not found
if (prefs.getBoolean(Constants.CALL_DONE, false)) {
// Make call
prefs.edit().putBoolean(Constants.CALL_DONE, true).apply();
}

ListView doesn't update unless screen orientation is changed

I'm trying to make an app which displays a list of books using google book's API. I'm able to populate the list items, but unless the screen orientation is changed the list views do not show up.
How can I update the list views as soon the search button is clicked?
Here is the entire project on github.
The AsyncTask is asynchronous, so the extractBooks method is called before the request is done.
You should call this method in doInBackground and binds data in list in the onPostExecute method.

Slow switching between android activities. Trying to use a Progression Dialog to show its loading

So this is a tuff one. I have a activity that calls a class that call a http downloader which is the async task. What I want to do is have a please wait loading screen notification for the user while the activity is getting all the data from the web. I don't have a clue on how to get the loading screen to be called. Usually you would have it in the async task but my async task does not have a view. How to best go about doing this?
Thank you for any help.
My http client does have a doInBackground() so should I put the on preExecute and postExecute there? But then how would my main method which is twice removed know when its down and when its not.
What is happening is that when a button is clicked it starts the activity and starts to process all the methods, but it linger on the first activity until everything is done loading. I don't want that I want the progress dialog. It just won't show. Any thoughts on this?
NEW UPDATES
Ok so now I got this set up a bit better so now at least the progress dialog now shows up. the problem is the Async task does not stop running.
You should use an AsyncTask. It doesn't have to have a view, it's just a background process just like a thread.
Refer to this on how to use an AsyncTask properly with a ProgressDialog.
Do your methods in the doInBackground() method of the AsyncTask.

how to render view in android based on the data received from server?

I am new to android development. I am trying to build basic shopping cart. I want to know how can I render view based on the data (product details) received from server. Because I will make a call to server in AsyncTask and I will have to wait for the data and then only I will be able to render them. So, I couldn't be able to figure out how would I manage it? Any help is appreciated.
Use a broadcastreceiver in the activity that contains the view you want to render.
Look at this answer:
How to send data to another app which is not started
Render your view in the onReceive() function of your BroadcastReceiver.
As you said you're going to use AsyncTask, you can render any view you want in the onPostExecute() method, which runs on the UIThread.
I would display a progressbar before you execute the AsyncTask and then AsyncTask's onPostExecute() method you would hide the progressbar and update the views with the requested data.

Saving Application State in Android

I am writing an app where I get all the data from the rest call and display all the data in a custom component list(based on Linear Layout) which is added to a LinearLayout. I write this code in onCreate of the activity.
The problem is when I switch activity using startActivity, and come back to the calling activity (using startActivity) then onCreate is called again. I see onPause, onStop called when I call other activity.
Is there any way that I can save the application's state?
Check this question: "How do I save an android application state".
Edit:
You can also avoid some calls to onCreate() by adding a
android:launchMode="singleTask"
to your Activity in the AndroidManifest.

Categories

Resources