I am using View Pager for showing 3 Fragments in an activity(Dashboard.java). The activity is loaded after clicking a button on previous activity(Login.java) and two of the fragments are showing a listview which is fetched from the server.
The problem is that it will take at least 3-7seconds to fetch the data from the server and populate as the listview. So I want to show a ProgressDialog to show a Loading status.
I have tried AsyncTask to load the data from the server in the fragment. It works but it show ProgressDialog only after a few time, just for a second before the list is populated. Also there is a delay on clicking the BUTTON to load the main Activity (Dashboard.java)
So there are two problems:
1. The progress dialog is appearing very late
2. There is a delay on the previous activity(Login.java) to MainActivity(Dashboard.java) (sometimes show black screen)
This is my AsyncTask in Fragment
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPostExecute(Void result) {
pd.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
pd.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID)); // id not using any
// where
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
// adding HashList to ArrayList
menuItems.add(map);
}
return null;
}
}.execute();
Actually where do i should use the ProgressDialog, on MainActivity (Dashboard.java) or Fragment.java ?
Please guide me to the right path.
EDIT
Actually what is the best way to load fragments with a progress dialog?
(The fragments are listview which populated from the server)
Nevermind, it was my mistake to write a method out side the doInBackground()