GithubHelp home page GithubHelp logo

loadersdemoitunes's Introduction

LoadersDemoiTunes

simplest demo app for loaders

Steps for implementing Loaders in Android

1- Make a Loader ,this is done by creating a class that extends AsyncTaskLoader and implement its methods onStartLoading() and loadInBackground()

public class SimpleLoader extends AsyncTaskLoader<String> {
private static final String TAG = "flow";
public SimpleLoader(Context context) {
    super(context);
}

@Override
protected void onStartLoading() {
    super.onStartLoading();
    forceLoad();
    Log.i(TAG, "onStartLoading: without ForceLoad it won't go to loadinBackground");
    Log.i(TAG, "onStartLoading: this will be called second "+ Thread.currentThread().getId());
}

@Override
public String loadInBackground() {
    Log.i(TAG, "loadInBackground: this will be called third "+ Thread.currentThread().getId());        
}
 } //class closing bracket

2- implement LoaderManager.LoadCallBacks interface by giving body to its methods.

public class MainActivity extends AppCompatActivity implements
    LoaderManager.LoaderCallbacks<D> 

3- inside onCreate() of Activity or anywhere else initiate a loader.

 getSupportLoaderManager().initLoader(0, null, this);

What will happen when we initiate Loader using Loader Manager

-once we intiate loader with unique id 0 using getSupportLoaderManager().initLoader(0, null, this); 
 the system will check if loader with id 0 exist ? if no than go to call back method onCreateLoader() of LoaderManager 
 if yes than go to call back method onLoadFinished() of LoaderManager.

Whats inside onCreateLoader()

-onCreateLoader() has return type <D>
-inside this method we create the Loader.
new SimpleLoader(this);

What next

-once Loader is created inside the onCreateLoader() ,the onStartLoading method of Loader is called .
-but after onStartLoading() method loadinBackground() is called only if we have put code forceLoad inside the onStartLoading method.
-loadinBackground() works on background thread.
-loadinBackground() has returns type <D> so it returns that value.
-this returned value goes directly to onCreateLoader() callback.
-at which point the Loader is Finished loading and onLoadFinished() callback is called.
-inside onLoadFinished() we can update the UI.

loadersdemoitunes's People

Contributors

theandroidboy avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.