ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Very strange action on back to previous fragment #192

Closed pishguy closed 5 years ago

pishguy commented 5 years ago

i have main fragment into MainActivity which that used and attached when i open application as HomeFragment, in this fragment i have one RecyclerView with some items and after clicking one of this list items and pushing fragment with this code:

public interface FragmentNavigation {
    void pushFragment(Fragment fragment);
}

i dont have any problem, but after click on Back button of phone in this RecyclerView i have more items, for example if fist start application i have 1 items and after pressing back button i have 2 items each pushing items and go back previous fragment 1 items automatically adding into this list

BaseFragment:

public class BaseFragment extends Fragment {
    private int mInt;
    public static String ARGS_INSTANCE = "ir.cafeAlachiq.instagramApplication.argsInstance";
    public FragmentNavigation fragmentNavigation;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args != null) {
            mInt = args.getInt(ARGS_INSTANCE);
        }
    }

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

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentNavigation) {
            fragmentNavigation = (FragmentNavigation) context;
        }
    }

    public interface FragmentNavigation {
        void pushFragment(Fragment fragment,FragNavTransactionOptions fragNavTransactionOptions);
    }
}

HomeFragment:

public class HomeFragment extends BaseFragment implements InstagramFeedsAdapter.OnClickListener {

    ...

    // constructor
    public static HomeFragment createInstance(int instance) {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(BaseFragment.ARGS_INSTANCE, instance);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.instagram_post_container, container, false);

        activity = getActivity();
        context = getActivity().getBaseContext();

        mediaUrls = new ArrayList<>();
        feedsSchema = new FeedsSchema();
        feedsSchema.setId(1);
        mediaUrls.add("http://gizasystems.com/wp-content/uploads/2014/08/natural-world-hero3.jpg");
        feedsSchema.setFeed_media_url(mediaUrls);
        feedsSchema.setFollowers_count(10);
        feedsSchema.setFollowings_count(20);
        feedsSchema.setPage_name("Hosna Pishguy");
        feedsSchema.setType(FeedsSchema.feed_types.IMAGE);
        items.add(feedsSchema);

        instagram_feeds = mView.findViewById(R.id.instagram_feeds);
        layoutManager = new LinearLayoutManager(context);
        instagram_feeds.setLayoutManager(layoutManager);
        instagram_feeds.setNestedScrollingEnabled(false);

        instagramFeedsAdapter = new InstagramFeedsAdapter(activity, context, this, items);

        displayMetrics = getResources().getDisplayMetrics();
        dpWidth = displayMetrics.widthPixels / displayMetrics.density;

        instagram_feeds.setAdapter(instagramFeedsAdapter);

        return mView;
    }

    public static void setUiWidgetsBehaviors(UiWidgetsBehaviors ui) {
        uiWidgetsBehaviors = ui;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
    }

    @Override
    public void onItemClick(View view, String transitionName, int position) {
        switch (view.getId()) {
            case R.id.page_thumbnail_avatar:
                FragNavTransactionOptions.Builder transition = new FragNavTransactionOptions.Builder().addSharedElement(new Pair<>(view, transitionName));
                fragmentNavigation.pushFragment(SampleFragment.createInstance(0), transition.build());
                break;
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList("items", new ArrayList<>(items));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null)
            items = savedInstanceState.getParcelableArrayList("items");
    }
}
ncapdevi commented 5 years ago

This is a question about Fragment lifecycles and is not specific to FragNav. Please keep questions/issues/bugs to the scope of the library.