Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is making network call safe in ArrayAdapter?

I have developed AutoCompleteTextView with Google Places API. When the user enters address, I'm making network call to API. I tried to simulate crash, but the request completes before I'm able to change the configuration.

    public class PlacesAutoCompleteAdapter extends ArrayAdapter<Prediction> implements Filterable {

    private List<Prediction> resultList;

    public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public Prediction getItem(int index) {
        return resultList.get(index);
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null) {
                    ShlepenApp.getGoogleRestClient().getPlaceList(GoogleService.API_KEY, constraint.toString(), new Callback<GooglePlacesListResponse>() {
                        @Override
                        public void success(GooglePlacesListResponse placesListResponse, Response response) {
                            Log.i("TAGE", "SUCCESS");
                            resultList = placesListResponse.getPredictions();
                            notifyDataSetChanged();
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            Log.i("TAGE", "FAILURE");
                            //TODO: Post something useful here.

                        }
                    });
                    // Assign the data to the FilterResults
                    filterResults.values = resultList;
                    filterResults.count = resultList.size();
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }
            }};
        return filter;
    }
}

EDIT: Can you please elaborate what actually happens during a network call when I rotate the device? Will this call be lost, and then recreated because letters are present in autotextview?

like image 853
max Avatar asked Oct 20 '22 16:10

max


1 Answers

Can you please elaborate what actually happens during a network call when I rotate the device? Will this call be lost, and then recreated because letters are present in autotextview?

I have not worked with Google Places API but I think this may help you:

when you rotate your device your activity is killed and your adapter but the thread that is doing network operation is exist and doing its job. After it has finished its job it is going to tell you the result(call one of the callback failure or success) but there is no adapter because the old one is garbage collected and the thread dose not access to the new adapter so it is calling callback method and in each of the method it is going to access the null object (private List<Prediction> resultList;).

like image 79
mmlooloo Avatar answered Oct 27 '22 17:10

mmlooloo