Reusing Fragments - 'Search' & Favourites'

If you recall in CoffeeMate 2.0 we used a Fragment to display our list of coffees inside our Home Screen. A Fragments main purpose is to be reused so we can use our existing Fragment in both our Search & Favourites Screens.

Search & Favourites Screens

The first thing we'll do is add the Fragment to our Search.java and Favourites.java Activity classes. This is pretty staright forward as we have already done this in our Home.java in CoffeeMate 2.0.

  coffeeFragment = CoffeeFragment.newInstance(); //get a new Fragment instance
            getFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_layout, coffeeFragment)
                    .commit(); // add/replace in the current activity

You can literally cut & paste the above code into both activity classes onResume() method and run the app, so see what you get?

And don't forget to use Android Studio's tools for overriding methods

and then

You should be seeing something like the following :

Obviously, our Favourites isn't filtered to show just 'Favourite Coffees' (we'll get to that later), but this does really illustrate the benefits of using Fragments when we can.

The next step is introducing the Filter so the user can view their Favourite Coffees (and eventually search).

results matching ""

    No results matching ""