package com.oreilly.supers;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HeroListFragment extends ListFragment {

    static interface HeroListListener {
        void itemClicked(long id);
    }

    private HeroListListener listener;

    public HeroListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        String[] names = new String[Hero.HEROES.length];
        for (int i = 0; i < Hero.HEROES.length; i++) {
            names[i] = Hero.HEROES[i].getName();
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                names
        );

        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    // Still need this for API < 23
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.v("ListFragment", "onAttach with activity=" + activity);
        listener = (HeroListListener) activity;
    }

    // Doesn't get called on API < 23
//    @Override
//    public void onAttach(Context context) {
//        super.onAttach(context);
//        listener = (HeroListListener) context;
//    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.v("ListFragment", "position=" + position + ", id=" + id);
        if (listener != null) {
            listener.itemClicked(id);
        }
    }
}

@@ -84,16 +84,73 @@
 agment;%0A
+import android.content.Context;%0Aimport android.os.Build;%0A
 import a
@@ -1235,53 +1235,50 @@
     
-// Still need this for API %3C 23%0A    @Override
+@Override @SuppressWarnings(%22deprecation%22)
 %0A   
@@ -1366,67 +1366,76 @@
     
-Log.v(%22ListFragment%22, %22onAttach with activity=%22 + activity)
+if (Build.VERSION.SDK_INT %3E Build.VERSION_CODES.LOLLIPOP_MR1) return
 ;%0A  
@@ -1491,48 +1491,8 @@
  %7D%0A%0A
-    // Doesn't get called on API %3C 23%0A//
     
@@ -1501,18 +1501,16 @@
 verride%0A
-//
     publ
@@ -1545,18 +1545,16 @@
 text) %7B%0A
-//
         
@@ -1578,18 +1578,16 @@
 ntext);%0A
-//
         
@@ -1625,18 +1625,16 @@
 ontext;%0A
-//
     %7D%0A%0A 

Implement alternative onAttach methods