0

I'm using volley to fetch data using volley and display in User Interface, and it takes time to load the data. What i want to happen is, I want to fetch the data in background during the splash screen or loading screen and display it in User interface. I want the fetching part to be done in another activity and it should be a background service and this should be called in "main activity" to populate the fields.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Sam Sundar
  • 61
  • 13

2 Answers2

0

use AsyncTask like that

 private class LongOperation extends AsyncTask<String, Void, String>
   {

    @Override
    protected String doInBackground(String... params)
    {
       //do your work here 


        return "Executed";
    }

    @Override
    protected void onPostExecute(String result)
    {
        progressBar.dismiss();

    }

    @Override
    protected void onPreExecute()
    {
        progressBar = ProgressDialog.show(getActivity(), null, "message...");
        progressBar.show();
    }

    @Override
    protected void onProgressUpdate(Void... values)
    {
    }
}
Ahsan Malik
  • 399
  • 2
  • 3
  • 13
0

You can try using the AsyncTask. So the process goes like that:

Example of AsyncTask usage.

Do it like this:

private class LoadDataBaseData extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        //Start loading your data 

        return "Data loaded";
    }

    @Override
    protected void onPostExecute(String result) {
       //Update your UI with data you loaded/start your activity with loaded data

    }

    @Override
    protected void onPreExecute(){     
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}
Anže Mur
  • 1,545
  • 2
  • 18
  • 37
  • The thing is i don't want to display any ProgressBar . when i click a button it shows updating and displays the data . the delay is 5 seconds. all i want to do is , when app starts , start loading the data in another activity and when the button is pressed display the data acquired instantly . No delay. Do the fetching part in background. – Sam Sundar Aug 02 '18 at 07:26
  • You can still do that with AsyncTask, ProgresBar/Dialog is just optional so it blocks UI for the time that data is loading. Use AsyncTask to load it, like I told you and just don't use any ProgresBars. You can start your AsyncTask OnCreate or OnStart and it should be OK. – Anže Mur Aug 02 '18 at 07:29
  • I added an example that would suit your needs. – Anže Mur Aug 02 '18 at 07:34