DMCApps / NavigationFragment

The purpose of this manager is to work with the ViewPager, Tabs and Navigation drawer to handle a single stack flow of fragments on the screen. It makes use of a main Fragment as a container and presents and hides fragments within it as children.
MIT License
28 stars 8 forks source link

childfragment manager are used for viewpager results crashes and unstable #2

Closed jjhesk closed 8 years ago

jjhesk commented 8 years ago

First of all, thank you for the amazing work. Here I got another challenge from the crashes on using v0.0.4. I have an example that the childfragment manager inside the fragment is used for supply the works for mutifragment holder such as ViewPager and SmartTabLayout. In this case I got all other things flashing in the page. what can i do to make it work?

Noticed that I set pager.setOffscreenPageLimit(0); because i need to constraint memory for another activities. In this layout I had to init a viewpager for display use return new FragmentPagerItemAdapter(getFragmentManager(), itemCreator.create()); or using return new FragmentPagerItemAdapter(getChildFragmentManager(), itemCreator.create()); this adapter is weakReference to holding all the config for the viewpager private final SparseArrayCompat<WeakReference<Fragment>> holder; into the SparseArrayCompatthis.holder = new SparseArrayCompat<>(pages.size());

resulted IOexecption: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name =

package com.hkm.hbstore.pages.homePage;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;

import com.dmcapps.navigationfragment.fragments.NavigationFragment;
import com.hkm.hbstore.R;
import com.hkm.hbstore.life.event.EBus;
import com.hkm.hbstore.life.event.ResponsiveBaring;
import com.hkm.hbstore.life.tB;
import com.hkm.hbstore.pages.testpage;
import com.hkm.layout.Module.Utils;
import com.hypebeast.sdk.api.model.symfony.Config;
import com.hypebeast.sdk.api.model.symfony.NavigationItem;
import com.hypebeast.sdk.application.hbx.ConfigurationSync;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by hesk on 22/12/15.
 */
@TargetApi(Build.VERSION_CODES.M)
public class main_three_tabs extends NavigationFragment
        implements
        View.OnScrollChangeListener,
        View.OnAttachStateChangeListener {

    private SmartTabLayout mTab;
    private ViewPager pager;
    private FragmentManager mChildFragmentManager;
    private int toolBarHeight, customBarHeight;
    //  private boolean remote_event_to_show, remote_event_to_force_hide;
    private ArrayList<String> tab_list = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.content_puzzle_v2, container, false);
    }

    @SuppressLint("ResourceAsColor")
    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onViewCreated(View v, Bundle b) {
        mTab = (SmartTabLayout) v.findViewById(R.id.feature_puzzle_smarttab);
        pager = (ViewPager) v.findViewById(R.id.feature_puzzle_viewpager);
        pager.setAdapter(getAdapter());
        pager.setOffscreenPageLimit(0);
        pager.setOnScrollChangeListener(this);
        mTab.setViewPager(pager);
        mTab.addOnAttachStateChangeListener(this);
        toolBarHeight = Utils.getToolbarHeight(getActivity());
        customBarHeight = Utils.getTabsHeight(getActivity());
    }

    public FragmentPagerItemAdapter getAdapter() {
        final FragmentPagerItems.Creator itemCreator = FragmentPagerItems.with(getActivity());
        try {
            final Config overhead_data = ConfigurationSync.getInstance().getFoundation().data;
            tab_list.clear();
            if (overhead_data.navigation.size() > 0) {
                final Iterator<NavigationItem> nk = overhead_data.navigation.iterator();
                while (nk.hasNext()) {
                    final NavigationItem g = nk.next();
                    if (g.name.equalsIgnoreCase("Featured")) {
                        itemCreator.add(tB.home.getStringId(), tB.home.getClazz());
                    } else {
                       /* itemCreator.add(
                                g.name,
                                template_product_display.class,
                                template_product_display.con_general(g.link)
                        );*/
                        itemCreator.add(g.name, testpage.class);
                        tab_list.add(g.link);
                    }
                }
            }
        } catch (Exception e) {
        }
        return new FragmentPagerItemAdapter(getFragmentManager(), itemCreator.create());
    }

    @Override
    public void onStop() {
        super.onStop();
        EBus.getInstance().unregister(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        EBus.getInstance().register(this);
    }

    @Subscribe
    public void onEvent(ResponsiveBaring.ScrollOnMove scroll) {
        if (scroll.isActionMove()) {
            mTab.setTranslationY(-scroll.getDistance() + toolBarHeight);
        }
        if (scroll.isActionShow()) {
            mTab.animate().translationY(toolBarHeight).setInterpolator(new DecelerateInterpolator(2)).start();
        }
        if (scroll.isActionHide()) {
            mTab.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
        }
    }

    boolean isFirstTab = true;

    @Override
    public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

        final int tab_width = 1080;
        if (scrollX < tab_width) {
            final int move = (tab_width - scrollX) / tab_width * toolBarHeight;
            if (!isFirstTab) {
                EBus.getInstance().post(new ResponsiveBaring.ScrollOnMove(ResponsiveBaring.ScrollOnMove.SHOW));
            }
            Log.d("smtab", move + " int");
            isFirstTab = true;
        } else {
            if (isFirstTab) {
                EBus.getInstance().post(new ResponsiveBaring.ScrollOnMove(ResponsiveBaring.ScrollOnMove.HIDE));
            }
            isFirstTab = false;
        }

    }

    @Override
    public void onViewAttachedToWindow(View v) {
        Log.d("smtab", "in");
        Log.d("smtab", v.getId() + ":" + v.toString());
    }

    @Override
    public void onViewDetachedFromWindow(View v) {
        Log.d("smtab", "out");
        Log.d("smtab", v.getId() + ":" + v.toString());
    }
}
DMCApps commented 8 years ago

Hey jjhesk,

Thanks a lot for the feedback and use of my library. I'm still very early stages (hence not even v1 yet) but I'm happy to try and help with anything you are encountering to improve the Manager and your experience with it.

I'm not quite sure I fully understand the set up that you have. It looks like you are adding this fragment with a ViewPager to a SingleStackFragmentManager at a previous point in the application?

One point in the code that I noticed that may give an issue is that since you are inside a fragment already, the ViewPager should be using getChildFragmentManager() instead of getFragmentManager() on this line in the getAdapter() method:

return new FragmentPagerItemAdapter(getFragmentManager(), itemCreator.create());

I ran into this issue previously (I can't remember the exact Exception) but changing the above line from getFragmentManager() to getChildFragmentManager() should clear up a possible issue. Give this a try and let me know if that clears it up. If not can you please post the full stack of the acceptation so that I can look further into what is going on?

Thanks, DMCApps

jjhesk commented 8 years ago

I dont know how to work with this. I found out that when i start to hide the app as i press the home button. It will crash and show java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = and it caused by : navigationfragment.manager.SingleStackNavigationManagerFragment

jjhesk commented 8 years ago

baconlmy48yhesk02222016141422

DMCApps commented 8 years ago

Updated so that the NavigationManagerFragments are all Serializable.

This should be fixed in 0.0.4.2

Please let me know if it persists after updating

DMCApps commented 8 years ago

Hey jjhesk,

So this issue seems to have been bigger than I expected. As I was using the Manager, I ran into this "NotSerializableException" on my home button press and any time I presented an activity. I was expecting that it was because my class was not Serializable hence I removed anything that wasn't serializable from my class and it was working.

The issue continues to happen though anytime you have a fragment with a property in it that is not serializable in it. So I have made a fix for this and it can be found as 0.0.5. All the methods for using the Manager remain the same so you should be good to go once you upgrade to 0.0.5!

Thanks for your continued use and pantience.

jjhesk commented 8 years ago

fixed cheers!