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.
            Asked
            
        
        
            Active
            
        
            Viewed 1,188 times
        
    0
            
            
         
    
    
        Jonathan Hall
        
- 75,165
- 16
- 143
- 189
 
    
    
        Sam Sundar
        
- 61
- 13
- 
                    2use Asynctask or threads or AsyncTaskLoader , – Muhammad Hassaan Aug 02 '18 at 07:15
- 
                    1Use `MVVM` pattern or `Rxandroid`, it will help you in reactive programming – Farhana Naaz Ansari Aug 02 '18 at 07:16
2 Answers
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:
- In onPreExecute() you can show a ProgresDialog or a ProgresBar to visualize your loading process
- in doInBackground(Params...) you start loading your data
- and in onPostExecute(Result) you display your data in UI and hide your ProgresDialog/ProgresBar.
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
-