Never start loading data when you need it. That’s too late.

PreLoader:A simple and powerful framework helps you preload data on Android, make your android activity launch more faster.

billy
2 min readFeb 2, 2019
Pre-load data before you need it immediately

If you know exactly what needs to be done next, you can do it now!

If you know exactly what data you need next, if you start preparing it now, can you get it more quickly when you need to use it.

PreLoader is an android framework that helps us pre-load data before we need to use it.

We can start a pre-load task like this:

//pre-load task can be started as early as possible
int preLoaderId = PreLoader.preLoad(new Loader());
//DataLoader, mock as load data from network
class Loader implements DataLoader<String> {
@Override
public String loadData() {
//this method runs in thread pool
// load data in this method synchronously
try {
Thread.sleep(600);
} catch (InterruptedException ignored) {
}
return "data from network server";
}
}
//startActivity with preLoadId
Intent intent = new Intent(this, PreLoadBeforeLaunchActivity.class);
intent.putExtra("preLoaderId", preLoaderId);
startActivity(intent);

Then start to listen data when you need to do this.

PreLoader.listenData(preLoaderId, new Listener());

//after data load completed,DataListener.onDataArrived(...) will be called to process data
class Listener implements DataListener<String> {
@Override
public void onDataArrived(String data) {
// this method runs on main thread, Handler is not required
Toast.makeText(activity, data, Toast.LENGTH_SHORT).show();
}
}

Powerful is: when we call PreLoader.listenData(preLoaderId, listener) to monitor data, if the preload task is completed, DataListener.onDataArrived(data) method will be executed immediately; otherwise, if the preload task is not completed, that method will be called after the preload task has completed.

You can do many preload things with PreLoader when you want to do so.

such as:

  • request network api
  • load network image
  • load local image
  • init database
  • load data for next page of ListView/RecyclerView before pull to load more
  • and so on…

--

--