/*** Copyright (c) 2008-2015 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Covered in detail in the book _The Busy Coder's Guide to Android Development_ https://commonsware.com/Android */ package com.commonsware.android.leak.configchange; import android.app.ListFragment; import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import java.util.ArrayList; public class AsyncDemoFragment extends ListFragment implements View.OnClickListener { private static final String[] items= { "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus" }; private ArrayList<String> model=new ArrayList<String>(); private ArrayAdapter<String> adapter=null; private AddStringTask task=null; private Button btnAgain=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); task=new AddStringTask(); task.execute(); adapter= new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, model); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return(inflater.inflate(R.layout.main, container, false)); } @Override public void onViewCreated(View v, Bundle savedInstanceState) { super.onViewCreated(v, savedInstanceState); getListView().setScrollbarFadingEnabled(false); setListAdapter(adapter); getAgain().setOnClickListener(this); if (task!=null) { getAgain().setEnabled(false); } } @Override public void onDestroy() { if (task!=null) { task.cancel(false); } super.onDestroy(); } private Button getAgain() { if (btnAgain==null) { btnAgain=(Button)getView().findViewById(R.id.again); } return(btnAgain); } @Override public void onClick(View v) { getAgain().setEnabled(false); adapter.clear(); task=new AddStringTask(); task.execute(); } class AddStringTask extends AsyncTask<Void, String, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... unused) { for (String item : items) { if (isCancelled()) break; publishProgress(item); SystemClock.sleep(400); } return(null); } @Override protected void onProgressUpdate(String... item) { if (!isCancelled()) { adapter.add(item[0]); } } @Override protected void onPostExecute(Void unused) { task=null; getAgain().setEnabled(true); } } }