Tom Hammond
Tom Hammond

Reputation: 6090

Android Implement Loading Screen

My app has a number of background tasks it needs to complete so I'm looking into doing a loading screen.

I think the route that I want to do is on entering my MainActivity set the content view to activity_loading and then start an AsyncTask that shows a progress bar and updates it as each of the tasks complete.

Then in the onPostExecute I set the content view to activity_main, while I still have all of my data and such in my activity.

Is this the best way to do it? Or is there a better route?

Upvotes: 1

Views: 966

Answers (3)

Kushal
Kushal

Reputation: 8508

You should not use setContentView() more with single activity. For your requirement, you should use Splash Screen when your activity is opened by user. You can display this for customized time i.e. 5000 ms, 7000 ms etc

While user is watching Splash Screen, we will start AsyncTask and start processing or loading of data. You can terminate Spash Screen from onPostExecute() and show your content view or fragment

Sample code from AndroidHive :

public class SplashScreen extends Activity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);


    // start your AsyncTask here
    new FetchData().execute();

}


 private class FetchData extends AsyncTask<Void, Void, Void> {


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // make calls to http network     

    }

    @Override
    protected Void doInBackground(Void... arg0) {
         // fetch data here
    }

    @Override
    protected Void onPostExecute(Void... arg0) {
         // change contentview
         // we call handler to show our main content and close this splash screen
   new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(SplashScreen.this, MainActivity.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, 2000);    // delay is good so that wrapping up of data is done
    }

}

Upvotes: 1

Shubham A.
Shubham A.

Reputation: 2534

Calling setContentView() more than one is a bad practice.
You should rather show ProgressBar as you are showing and than :

  • If you are using a ListView or a GridView, you should rather call the notifyDataSetChanged() to reflect the changes.
  • You should try using Fragments to dynamically replace the loading portion of the screen with the dynamic Fragment.

Upvotes: 1

rahul.taicho
rahul.taicho

Reputation: 1379

You can manage it by including the loading layout inside a FrameLayout of your activity and then toggling its visibility. Calling setContentView seems a bit unnecessary. Also ofc make sure your FrameLayout is the last child view in your Activity's layout. It'd become easier to manage this way as you'd know all your activities with AsyncTask will have a loading layout that can get toggled.

Upvotes: 1

Related Questions