0

I have a Tabbed Activity hosting a viewpager (auto-generated with eclipse wizard). I slightly modified just the auto generated FragmentPagerAdapter in order to instantiate a specific fragment on each tab. Initially i had 4 tab, 3 fragments for the first tabs and still the auto generated placeholder in the last one, and they worked. When i replaced the last placeholder with an instance of one of my Fragment i faced the following problem: the fragment in one of the last two tabs stays blank, as follow:

_____1st TAB___|____2nd TAB_|___3rd TAB_____|____4th TAB____|

[MasterFragment][RequestMap][MasterFragment][MasterFragment]

This stays blank_____________________|

Moreover, if i make a "random" sequence of actions (change tabs, click on the buttons to perform some actions,...) the fragment that doesn't show up swaps, and becomes the last one as follow:

_____1st TAB___|____2nd TAB_|___3rd TAB_____|____4th TAB____|

[MasterFragment][RequestMap][MasterFragment][MasterFragment]

This stays blank__________________________________|

Here's the code for my FragmentPagerAdapter:

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case REQUEST_TAB:
            masterFragment = new MasterFragment(MasterFragment.ALL_REQUEST);
            return masterFragment;
        case MAP_TAB:
            requestMap = new RequestMap();
            return requestMap;
        case OWNER_TAB:
            masterFragmentOwner = new MasterFragment(MasterFragment.OWNER_REQUEST);
            System.out.println("I should have created the Owner MasterFragment");
            return masterFragmentOwner;
        case JOINED_TAB:
            masterFragmentJoined = new MasterFragment(MasterFragment.JOINED_REQUEST);
            return masterFragmentJoined;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case REQUEST_TAB:
            return getString(R.string.request_tab_title).toUpperCase(l);
        case MAP_TAB:
            return getString(R.string.map_tab_title).toUpperCase(l);
        case OWNER_TAB:
            return getString(R.string.owner_tab_title).toUpperCase(l);
        case JOINED_TAB:
            return getString(R.string.joined_tab_title).toUpperCase(l);
        default:
            return "";
        }
    }
}

I omit the code regarding the setup of the viewPager because is the same auto generated from the eclipse wizard. While here's the code for the MasterFragment class:

package it.polimi.frontend.fragment;

import it.polimi.appengine.entity.manager.model.Request;
import it.polimi.appengine.entity.manager.model.User;
import it.polimi.frontend.activity.R;
import it.polimi.frontend.util.QueryManager;
import it.polimi.frontend.util.QueryManager.OnRequestLoadedListener;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MasterFragment extends Fragment implements OnRequestLoadedListener, RequestList.OnRequestSelectedListener, RequestDetail.OnUserClickedListener{

private boolean twoPane;
private static View view;
public final static int ALL_REQUEST=0;
public final static int OWNER_REQUEST=1;
public final static int JOINED_REQUEST=2;
private int mode;

public MasterFragment(){
    this.mode=0;
}
public MasterFragment(int mode){
    this.mode=mode;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    QueryManager.getInstance().addListener(this);
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.fragment_master,container, false);
        List<Request> requests = new ArrayList<Request>();
        RequestList requestListFragment = null;
        switch (mode) {
        case OWNER_REQUEST:
            requests = QueryManager.getInstance().getCurrentUser().getRequests();
            System.out.println("Dovrei aver recuperato le richieste dell'owner");
            break;
        case JOINED_REQUEST:
            String mail=null;
            if (QueryManager.getInstance().getCurrentUser()!=null){
                User current =QueryManager.getInstance().getCurrentUser();
                mail = current.getPwAccount() != null ? 
                        current.getPwAccount()
                        : current.getGmailAccount() != null ?
                                current.getGmailAccount() 
                                : current.getFbAccount() != null ?
                                        current.getFbAccount()
                                        : null;
                requests = QueryManager.getInstance().getUserPartecipation(mail);
            }
            else
                requests = new ArrayList<Request>();
            break;
        default: //ALL_REQUEST case and all the other possibilities
            requests = QueryManager.getInstance().getRequests();
            break;
        }
        requestListFragment = new RequestList(requests, mode);
        if (view.findViewById(R.id.detail_container) != null) {//TABLET CASE
            twoPane = true;
            getChildFragmentManager().beginTransaction()
            .replace(R.id.request_list_container,requestListFragment,RequestList.ID)
            .commit();

        } else { //PHONE CASE:
            getChildFragmentManager().beginTransaction()
            .replace(R.id.container,requestListFragment,RequestList.ID)
            .commit();
        }
    } catch (InflateException e) {
        // is already there, just return view as it is
        e.printStackTrace();
    }
    return view;
}

@Override
public void onRequestSelected(int position, Request request) {
    if (twoPane) {
        DetailContainerFragment detailContFrag = new DetailContainerFragment(request,mode);
        getChildFragmentManager().beginTransaction()
        .replace(R.id.detail_container, detailContFrag, DetailContainerFragment.ID).commit();
    } else {
        switch (mode) {//This empty switch if for future changes
        case OWNER_REQUEST:
            break;
        case JOINED_REQUEST:
            break;
        default://ALL_REQUEST               
            break;
        }
        RequestDetail fragment = new RequestDetail(request,mode);
        Fragment reqList=getChildFragmentManager().findFragmentByTag(RequestList.ID);

        getChildFragmentManager().beginTransaction()
        .hide(reqList)
        .addToBackStack(RequestDetail.ID)
        .add(R.id.container,fragment,RequestDetail.ID)
        .commit();
    }
}

@Override
public void onUserClicked(User user,String requestId) {
    if (!twoPane) {
        FeedbackDetail fragment = new FeedbackDetail(user,this.mode,requestId);
        Fragment reqDetail=getChildFragmentManager().findFragmentByTag(RequestDetail.ID);

        getChildFragmentManager().beginTransaction()
        .hide(reqDetail)
        .addToBackStack(FeedbackDetail.ID)
        .add(R.id.container,fragment,FeedbackDetail.ID)
        .commit();

    } else {
        /*DetailContainerFragment should take care of it*/
    }
}
@Override
public void onRequestLoaded(List<Request> requests) {
    System.out.println("Ho caricato: "+requests.size());
    RequestList requestListFragment = (RequestList)getChildFragmentManager().findFragmentByTag(RequestList.ID);
    switch (mode) {//Also this switch is for future changes, but the requests list is anyway fetched and assigned to RequestList above
    case OWNER_REQUEST:
        //TODO
        break;
    case JOINED_REQUEST:
        //TODO
        break;
    default: //ALL_REQUEST
        if (requestListFragment!=null)
            requestListFragment.setRequestAdapter(requests);
        break;
    }
}

}

If i'm missing something important please, let me know. Thank you all in advance

EDIT: I forgot to say that the System.out that i placed show up in the console, so the "blank" fragment should have been created, and it should have passed through his onCreateView().

1 Answers1

0

I found the bug. The problem was

private static View view;

because of the static properties. Removing "static" it worked again. Actually i can't explain even now how it could have worked for a while on the first and last tabs and not in the third, because the static attributes should be shared among all instances of the class. Because of the internals of the ViewPager the problem seemed to show up only on adjacent identical MasterFragment, if they where interleaved with any other fragment it worked.