javaandroidandroid-fragmentsflipview

Detach isn't working (Two Fragments with FlipView)


I'm working with fragments and flipview but I'm having some problems with this.

When my app opens the first screen show my feed with the FlipView. When I choose another tab of my fragments the fragment of the feed didn't hide or detach. I debugged many times and the code arrives in the detached line but didn't work.

Here is my code

MainActivity class


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FontManager.getInstance().initialize(this, R.xml.fonts);
        setContentView(R.layout.activity_main);

        mFBSession = new FBSessionsManager();
        mActionBar = getActionBar();
        fm = getFragmentManager();
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        TabHost.OnTabChangeListener mTabChangeListener = new TabHost.OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                ft = fm.beginTransaction();
                ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);

                FeedPrincipalList mFeedFragment = (FeedPrincipalList) fm.findFragmentByTag(TAB_TAG_FEED);
                EverythingFragmentList mEverythingFragment = (EverythingFragmentList) fm.findFragmentByTag(TAB_TAG_ALL_EVERYTHING);
                PaymentFragment mPaymentFragment = (PaymentFragment) fm.findFragmentByTag(TAB_TAG_PAYMENT);
                ConsultFragmentList mConsultFragmentList = (ConsultFragmentList) fm.findFragmentByTag(TAB_TAG_M_CONSULT);
                ProfileFragmentList mProfileFragmentList = (ProfileFragmentList) fm.findFragmentByTag(TAB_TAG_M_PROFILE);

                if (mFeedFragment != null){
                    ft.detach(mFeedFragment);
//                    ft.commit();
                }

                if (mEverythingFragment != null)
                    ft.detach(mEverythingFragment);

                if (mPaymentFragment != null)
                    ft.detach(mPaymentFragment);

                if (mConsultFragmentList != null)
                    ft.detach(mConsultFragmentList);

                if (mProfileFragmentList != null){
                    ft.detach(mProfileFragmentList);
//                    ft.commit();
                }

                if (tabId.equalsIgnoreCase(TAB_TAG_FEED)) {
                    manageContextualActions(true, false);
                    if (mFeedFragment == null) {
                        mFeedFragment = new FeedPrincipalList();
                        ft.add(R.id.realTabContent, mFeedFragment, TAB_TAG_FEED);
                    } else {
                        mFeedFragment = new FeedPrincipalList();
                        ft.attach(mFeedFragment);
                    }
                }

                if (tabId.equalsIgnoreCase(TAB_TAG_ALL_EVERYTHING)) {
                    manageContextualActions(false, true);
                    if (mEverythingFragment == null) {
                        mEverythingFragment = new EverythingFragmentList();
                        ft.replace(R.id.realTabContent, mEverythingFragment, TAB_TAG_ALL_EVERYTHING);
                    } else {
                        ft.attach(mEverythingFragment);
                    }
                }

                if (tabId.equalsIgnoreCase(TAB_TAG_PAYMENT)) {
                    manageContextualActions(false, true);
                    if (mPaymentFragment == null) {
                        mPaymentFragment = new PaymentFragment();
                        ft.replace(R.id.realTabContent, mPaymentFragment, TAB_TAG_PAYMENT);
                    } else {
                        ft.attach(mPaymentFragment);
                    }
                }

                if (tabId.equalsIgnoreCase(TAB_TAG_M_CONSULT)) {
                    manageContextualActions(false, true);
                    if (mConsultFragmentList == null) {
                        mConsultFragmentList = new ConsultFragmentList();
                        ft.replace(R.id.realTabContent, mConsultFragmentList, TAB_TAG_M_CONSULT);
                    } else {
                        ft.attach(mConsultFragmentList);
                    }
                }

                if (tabId.equalsIgnoreCase(TAB_TAG_M_PROFILE)) {
                    manageContextualActions(false, false);
                    if (mProfileFragmentList == null) {
                        mProfileFragmentList = new ProfileFragmentList();
                        ft.replace(R.id.realTabContent, mProfileFragmentList, TAB_TAG_M_PROFILE);
                    } else {
                        mProfileFragmentList = new ProfileFragmentList();
                        ft.attach(mProfileFragmentList);
                    }
                }

                ft.commit();
            }
        };

        mTabHost.setOnTabChangedListener(mTabChangeListener);
        createAndConfigureTabs();
    }

@Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void setupActionBar(int stringTitleID) {
        mActionBar.setDisplayShowCustomEnabled(true);
        mActionBar.setDisplayShowTitleEnabled(false);
        mActionBar.setDisplayUseLogoEnabled(false);

        final LayoutInflater inflater = LayoutInflater.from(this);
        final View customTitle = inflater.inflate(R.layout.custom_title, null);

        ((TextView) customTitle.findViewById(R.id.customTitleActionbar)).setText(getResources().getString(stringTitleID));

        mActionBar.setCustomView(customTitle);
    }

    private void createAndConfigureTabs() {
        mTabFeed = mTabHost.newTabSpec(TAB_TAG_FEED);
        mTabFeed.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_feed));
        mTabFeed.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabFeed);

        mTabAllEverything = mTabHost.newTabSpec(TAB_TAG_ALL_EVERYTHING);
        mTabAllEverything.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_all_everything));
        mTabAllEverything.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabAllEverything);

        mTabCameraAndPayment = mTabHost.newTabSpec(TAB_TAG_PAYMENT);
        mTabCameraAndPayment.setIndicator(null, getResources().getDrawable(R.drawable.btn_camera));
        mTabCameraAndPayment.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabCameraAndPayment);

        mTabConsult = mTabHost.newTabSpec(TAB_TAG_M_CONSULT);
        mTabConsult.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_consult));
        mTabConsult.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabConsult);

        mTabMyProfile = mTabHost.newTabSpec(TAB_TAG_M_PROFILE);
        mTabMyProfile.setIndicator(null, getResources().getDrawable(R.drawable.custom_btn_profile));
        mTabMyProfile.setContent(new TabContentCreator(this));
        mTabHost.addTab(mTabMyProfile);

        mTabWidget = mTabHost.getTabWidget();
        for (int i = 0; i minor 5; i++) {
             View v = mTabWidget.getChildAt(i);
            v.setBackgroundResource(android.R.drawable.screen_background_light_transparent);

            if (i == 4)
                v.setPadding(0, 0, 0, 12);
        }
    }

private class TabContentCreator implements TabHost.TabContentFactory {
        private Context mContext;

        public TabContentCreator(Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            return v;
        }
    }

FeedFragment class


public class FeedPrincipalList extends android.app.Fragment implements FlipView.OnFlipListener, FlipView.OnOverFlipListener, FlipView.OnClickListener {

    private static int timelinePage = 1, mPageCount = 0;
    private FlipView mFlipView;
    private FeedPrincipalAdapter mAdapter;
    private FBSessionsManager fbSessionsManager;
    private List mList;
    private Context context;
    private int posicaoTela;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity().getApplicationContext();

        View view = inflater.inflate(R.layout.feed_flipview, container);

        ((MainFragmentActivity) getActivity()).setupActionBar(R.string.title_fragment_feed);

        mFlipView = (FlipView) view.findViewById(R.id.fv_feedFlipView_FlipView);
        mFlipView.setOnFlipListener(this);
        mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
        mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
        mFlipView.setOnOverFlipListener(this);
        mFlipView.setOnClickListener(this);

        fbSessionsManager = new FBSessionsManager(getActivity());

        if (mList == null)
            new LoadFeedPrincipalData().execute();

        return super.onCreateView(inflater,container,savedInstanceState);
    }

    @Override
    public void onFlippedToPage(FlipView v, int position, long id) {
        Log.i("pageflip", "Page: " + position);
        posicaoTela = position;
        if(position > mFlipView.getPageCount()-3 && mFlipView.getPageCount() {

        private UtilWS ws;
        private JSONObject toSend;
        private JsonArray rsArray;
        private JsonParser parser;

        public LoadFeedPrincipalData() {
            ws = new UtilWS();
            toSend = new JSONObject();
            parser = new JsonParser();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                toSend.put("token", fbSessionsManager.getStoredPrivateSession()[0]);
                toSend.put("userId", fbSessionsManager.getStoredPrivateSession()[1]);
                toSend.put("page", timelinePage);

                String[] jsonResult = ws.post(UtilWS.URL_TIMELINE, toSend.toString());
                if (jsonResult[0].equals("200")) {
                    JsonObject temp = parser.parse(jsonResult[1]).getAsJsonObject();
                    Log.i("JSON",": "+temp.toString());
                    if (temp.get("success").getAsBoolean()) {
                        rsArray = temp.get("looks").getAsJsonArray();
                        if (mPageCount == 0)
                            mPageCount = temp.get("pageCount").getAsInt();
                        for (JsonElement el : rsArray) {
                            if (mList == null)
                                mList = new ArrayList();
                            temp = (JsonObject) el;
                            FeedPrincipalTO tempTudo = new FeedPrincipalTO(temp.get("lookId").getAsString(), temp.get("photoUrl1").getAsString(), temp.get("photoUrl2").getAsString(), temp.get("jaVotou").getAsBoolean(),temp.get("photoVoted").getAsInt() ,temp.get("photo1Total").getAsString(), temp.get("photo2Total").getAsString());
                            mList.add(tempTudo);
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            if (mList != null) {
                Log.i("mList","mLista");
                mAdapter = new FeedPrincipalAdapter(context,mList);
                mAdapter.setCallback(new FeedPrincipalAdapter.Callback() {
                    @Override
                    public void onPageRequested(int page) {
                        mFlipView.smoothFlipTo(page);
                    }

                    @Override
                    public void voteUp(int position) {
                        Log.i("TESTE","position = " + posicaoTela);
                        mAdapter.getView(position, null, null).postInvalidate();
//      mAdapter.notifyDataSetChanged();
                    }
                });
                if (timelinePage == 1) {
                    mFlipView.setAdapter(mAdapter);
//                    initListenerList();
                } else {
                    ((FeedPrincipalAdapter) mFlipView.getAdapter()).updateList(mList);
                    ((FeedPrincipalAdapter) mFlipView.getAdapter()).notifyDataSetChanged();
                }
            } else {
                mFlipView.setAdapter(new FeedPrincipalAdapter(context,null));
            }
        }

//        private void initListenerList() {
//            getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
//                @Override
//                public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//                }
//
//                @Override
//                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//                    if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
//                        if (timelinePage minor mPageCount) {
//                            timelinePage++;
//                            new LoadFeedPrincipalData().execute();
//                        }
//                    }
//                }
//            });
//        }
    }
}

Please, help me with this!!!

I'm using the FlipView https://github.com/emilsjolander/android-FlipView

Thanks!!!


Solution

  • Finally I solved this problem.

    First I changed my Main Class a little.

    I changed this two ifs (this fragments uses FlipView) in the MainActivity class.

    
    if (tabId.equalsIgnoreCase(TAB_TAG_FEED)) {
        manageContextualActions(true, false);
        if (mFeedFragment == null) {
            mFeedFragment = new FeedPrincipalList();
            ft.add(R.id.realTabContent, mFeedFragment, TAB_TAG_FEED);
        } else {
            // mFeedFragment = new FeedPrincipalList();
            ft.attach(mFeedFragment);
        }
    }
    
    if (tabId.equalsIgnoreCase(TAB_TAG_M_PROFILE)) {
        manageContextualActions(false, false);
        if (mProfileFragmentList == null) {
            mProfileFragmentList = new ProfileFragmentList();
            ft.add(R.id.realTabContent, mProfileFragmentList, TAB_TAG_M_PROFILE);
        } else {
            // mProfileFragmentList = new ProfileFragmentList();
            ft.attach(mProfileFragmentList);
        }
    }
    

    Second I changed my method onCreateView of my FeedFragment class.

    
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            context = getActivity().getApplicationContext();
    
            ((MainFragmentActivity) getActivity()).setupActionBar(R.string.title_fragment_feed);
    
            fbSessionsManager = new FBSessionsManager(getActivity());
    
            if (mList == null){
                view = inflater.inflate(R.layout.feed_flipview, container,true);
                mFlipView = (FlipView) view.findViewById(R.id.fv_feedFlipView_FlipView);
                mFlipView.setOnFlipListener(this);
                mFlipView.setOverFlipMode(OverFlipMode.RUBBER_BAND);
                mFlipView.setEmptyView(view.findViewById(R.id.empty_view));
                mFlipView.setOnOverFlipListener(this);
                new LoadFeedPrincipalData().execute();
            }
    
            return super.onCreateView(inflater,container,savedInstanceState);
        }
    

    You can see the difference. I only inflate the view in the first time. After this my mList(it's a List of MyGetSetClass) it's not null anymore.

    And to finish I Override this two methods on my FeedFragment class.

    
    @Override
        public void onDetach() {
            super.onDetach();
    //        Log.i("onDetach", "FeedPrincipalList");
            mList = null;
            timelinePage = 1;
            mPageCount = 0;
        }
    
        @Override
        public void onStart() {
            super.onStart();
    //        Log.i("onStart", "FeedPrincipalList");
            this.mFlipView.setVisibility(View.VISIBLE);
        }
    
        @Override
        public void onStop() {
            super.onStop();
    //        Log.i("onStop","FeedPrincipalList");
            this.mFlipView.setVisibility(View.INVISIBLE);
        }
    

    In the detach method I clean all my static variables that I will use when I start the app again.

    The onStart and the onStop I use to change the Visibility of my FlipView.

    That's it.

    I hope this help everyone!!!