Skip to content

0. Toro starting guide

Nam Nguyen Hoai edited this page Oct 23, 2016 · 1 revision

Advance users may want to understand the following basic components and terms in Toro:

Top level class, the heart of this library. Toro class comes with the initialisation blocks, registering/unregistering support for Views and many other useful components.

Init Toro to start the magic in your App

  • Init Toro to your Application class is a good place to start with. I recommend this usage.
public class MyAwesomeApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    Toro.init(this);
  }
}

Don't forget the Manifest entry for your Application

  • If you don't extend Application, simply attach Toro from your Activity's onCreate()
public class AwesomeActivity extends AppCompatActivity {

  @Override protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_home);
      Toro.attach(this);
  }
}
  • If you attached Toro to an Activity, don't forget to detach Toro to prevent unexpected result.
@Override protected void onDestroy() {
    Toro.detach(this);
    super.onDestroy();
}

Register/Unregister RecyclerView to Toro

Call Toro#register(RecyclerView) to activate Toro for a specific RecyclerView, and Toro#unregister(RecyclerView) to deactivate it.

For best practice, user should register the View right after completing that View's setup (LayoutManager setup, Adapter setup, ...).

After all, unregistering the RecyclerView in preferable Lifecycle event (Fragment#onPause() or Fragment#onDestroyView() or Activity#onDestroy, before Toro#detach). Android API 24 and above has a different behaviour, checkout Demo App for best practice.

A simple Fragment which registered its RecyclerView to Toro would look like this:

public class SimpleFragment extends Fragment {

  RecyclerView mRecyclerView;
  
  // Setup mRecyclerView in #onCreateView and/or #onViewCreated

  @Override public void onResume() {
    super.onResume();
    Toro.register(mRecyclerView);
  }

  @Override public void onPause() {
    Toro.unregister(mRecyclerView);
    super.onPause();
  }
}

That's the smallest code blocks to start using Toro in your App.