I have a tabbed application using ActionBarSherlock. Tabs in the ActionBar are each fragments. One of the fragments contains a viewpager which shows a number of fragments - each containing an image from a URL (using a variant of ImageView).
Whole thing works nicely the first time you click on the tab - it shows the pager which shows the images. 2nd time you click the tab (after clicking other tabs) nothing happens - pager does not show up and screen remains blank (besides actionBar). Code is attached below. It seems FragmentPagerAdapter is being created but its not paging through items 2nd time around.
What am I doing wrong ?
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.actionbarsherlock.app.SherlockFragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SurfaceChartFragment extends SherlockFragment {
private ViewPager mPager;
private SurfacePagerAdapter mAdapter;
private static String [] urls = { "gif url 1", "gif url 2 etc." };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.surface_pager, container, false);
mPager = (ViewPager) V.findViewById(R.id.surface_viewpager);
mAdapter = new SurfacePagerAdapter(getActivity().getSupportFragmentManager());
new setAdapterTask().execute();
return V;
}
private class setAdapterTask extends AsyncTask<Void,Void,Void>{
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
mPager.setAdapter(mAdapter);
}
}
static final class SurfacePagerAdapter extends FragmentPagerAdapter { //
public SurfacePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return urls.length;
}
@Override
public Fragment getItem(int position) {
SurfaceFragment f = new SurfaceFragment();
f.url = urls[position];
f.position = position;
return f;
}
}
public static class SurfaceFragment extends SherlockFragment {
String url = "";
Integer position = 0;
public SurfaceFragment() {
// setRetainInstance(true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setHasOptionsMenu(true);
}
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Log.e("Exception",e.getMessage());
return null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
Bitmap bimage= getBitmapFromURL(url);
image.setImageBitmap(bimage);
return image;
}
}
}