I have a ViewPager using a FragmentStatePagerAdapter inside an Activity which have to display 3 fragments, for now only the first one is implemented. The first fragment uses an asyncTask to get a list of categories and display them in a ListView.
The ViewPager uses a FragmentPagerAdapter to display the items.
The problem comes when the user hits the button on the tab bar to go back on the first activity. The second time we go on the activity with the fragments, I can't update the view, there is a ListView that I update and ProgressBar on which I call setVisibility but nothing happens.
Why is the view updating only the first time?
Activity which contains the ViewPager:
public class BoutiqueActivity extends AppCompatActivity {
protected ViewPager viewPager;
protected TabLayout tabLayout;
protected Bundle savedInstanceState;
protected ViewPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boutique);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.viewPager = this.findViewById(R.id.viewPager);
this.viewPager.setSaveFromParentEnabled(false);
this.savedInstanceState = savedInstanceState;
this.adapter = new ViewPagerAdapter(getSupportFragmentManager());
if (savedInstanceState == null){
this.setupViewPager();
}else {
Integer count = savedInstanceState.getInt("tabsCount");
String[] titles = savedInstanceState.getStringArray("titles");
for (int i = 0; i < count; i++) {
this.adapter.addFragment(getFragment(i), titles[i]);
}
}
}
@Override
public void onStart(){
super.onStart();
viewPager.setAdapter(adapter);
this.tabLayout = this.findViewById(R.id.tabLayout);
this.tabLayout.setupWithViewPager(viewPager);
}
private Fragment getFragment(int position){
return savedInstanceState == null ? adapter.getItem(position) : getSupportFragmentManager().findFragmentByTag(getFragmentTag(position));
}
private String getFragmentTag(int position) {
return "android:switcher:" + R.id.viewPager + ":" + position;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tabsCount", adapter.getCount());
outState.putStringArray("titles", adapter.getFragmentTitles().toArray(new String[0]));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == android.R.id.home) {
this.finish();
}
return super.onOptionsItemSelected(item);
}
private void setupViewPager() {
this.adapter.addFragment(new CategorieFragment(), "Catégories");
this.adapter.addFragment(new ArticleFragment(), "Articles");
this.adapter.addFragment(new PromotionFragment(), "Promotions");
}}
Fragment:
public class CategorieFragment extends Fragment implements ActiviteEnAttenteAvecResultat<Categorie> {
protected ArrayList<Categorie> categories;
protected ListView listView;
protected Drawable substitut;
protected ProgressBar loader;
public CategorieFragment(){};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
View view = inflater.inflate(R.layout.fragment_categorie, null);
try {
this.substitut = Drawable.createFromStream(getActivity().getAssets().open("cintre.png"), null);
} catch (IOException e){}
return view;
}
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
this.categories = new ArrayList<>();
}
@Override
public void onStart(){
super.onStart();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.listView = view.findViewById(R.id.liste);
this.loader = view.findViewById(R.id.loader);
CategorieAdapter categorieAdapter = new CategorieAdapter(getActivity(), categories, substitut);
this.listView.setAdapter(categorieAdapter);
CategorieDAO.getInstance(this, this.loader, this.listView).findAll();
}
@Override
public void notifyRetourRequete(Categorie result){
}
@Override
public void notifyRetourRequeteFindAll(ArrayList<Categorie> liste){
this.categories.clear();
this.categories.addAll(liste);
((BaseAdapter) this.listView.getAdapter()).notifyDataSetChanged();
}
@Override
public void afficheLoader(){
this.loader.setVisibility(View.VISIBLE);
this.listView.setVisibility(View.INVISIBLE);
}
@Override
public void cacheLoaderAfficheContenu() {
this.loader.setVisibility(View.INVISIBLE);
this.listView.setVisibility(View.VISIBLE);
}}
Adapter:
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
protected final List<Fragment> fragmentList = new ArrayList<>();
protected final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager){
super(manager);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position){
return fragmentTitleList.get(position);
}
public void addFragment(Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
public List<String> getFragmentTitles(){
return this.fragmentTitleList;
}}
AsyncTask
public class HTTPRequest<T extends Object> extends AsyncTask<String, Void, String> {
protected ProgressBar loader;
protected ListView listView;
protected ActiviteEnAttenteAvecResultat activite;
protected DAO dao;
protected String method;
protected String error;
protected Object data;
protected Class deserializationClass;
public HTTPRequest(ActiviteEnAttenteAvecResultat activite, DAO dao, String method, Object data, Class deserializationClass, ProgressBar loader, ListView listView){
this.activite = activite;
this.dao = dao;
this.method = method;
this.data = data;
this.deserializationClass = deserializationClass;
this.loader = loader;
this.listView = listView;
}
public HTTPRequest(ActiviteEnAttenteAvecResultat activite, DAO dao, String method, Object data, Class deserializationClass){
this.activite = activite;
this.dao = dao;
this.method = method;
this.data = data;
this.deserializationClass = deserializationClass;
}
@Override
protected void onPreExecute(){
this.afficheLoader();
}
@Override
protected String doInBackground(String... urls) {
String urlRequete = urls[0];
StringBuffer resultat = new StringBuffer(1024);
try{
final HttpURLConnection connection = (HttpURLConnection) new URL(urlRequete).openConnection();
connection.setReadTimeout(1000);
connection.setConnectTimeout(1500);
connection.setRequestMethod(this.method);
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
String line = "";
while((line = in.readLine()) != null){
resultat.append(line);
}
in.close();
} catch (Exception e){
this.error = "Error : " + e.getMessage();
return null;
}
return resultat.toString();
}
@Override
protected void onPostExecute(String result){
Gson gson = new Gson();
ArrayList<T> liste = new ArrayList<>();
if(result != null){
switch (this.method){
case (HTTPRequestMethod.GET):
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(result).getAsJsonArray();
for (int i = 0; i < array.size(); i++) {
Object object = gson.fromJson(array.get(i), this.deserializationClass);
liste.add((T) object);
}
activite.notifyRetourRequeteFindAll(liste);
break;
}
} else {
}
this.cacheLoaderAfficheContenu();
}
public void afficheLoader(){
this.loader.setVisibility(View.VISIBLE);
this.listView.setVisibility(View.INVISIBLE);
}
public void cacheLoaderAfficheContenu() {
this.loader.setVisibility(View.INVISIBLE);
this.listView.setVisibility(View.VISIBLE);
}}