0

when i'm selecting a month on a calendarview in a different fragment or when i rotate the screen, i'm losing the "on hold" fragments. but when i start to swipe all the fragment are acting normally again. I suppose it's related to the life cycle of my application but i'm having trouble to find how to solve this.

i'm using also

            android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

in my manyfest to keep the state of my buttons on one of my fragment.

what did i miss ?

MattAzerty
  • 13
  • 1
  • 5
  • 1
    What do you mean by loosing fragment ? you have to save your fragment State properly to restore it .. Can you add a [mcve] with question . – ADM Aug 04 '22 at 10:44

2 Answers2

1

you can use savedInstanceState or viewModel (I would suggest for first one) in android. So, viewPager has ViewPager.onPageChangeListener(refer to this SO answer https://stackoverflow.com/a/11294494/12649627). So, essentially you activity or fragment should look like this

Java soln:

public class MyFragment extends Fragment {
    private int pagerPosition = 0;
    private final static String PAGER_POSITION_TAG = "PAGER_POSITION_TAG";
    private ViewPager2 myViewPager;
    private ViewPager2.OnPageChangeCallback callback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            pagerPosition = position;
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(PAGER_POSITION_TAG, 0);
        }
        return inflater.inflate(R.layout.my_fragment, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myViewPager = view.findViewById(R.id.my_view_pager);
        myViewPager.setCurrentItem(pagerPosition);
        myViewPager.registerOnPageChangeCallback(callback);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(PAGER_POSITION_TAG, pagerPosition);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        myViewPager.unregisterOnPageChangeCallback(callback);
    }
}

P.s.: I found out that there is something known as registerOnPageChangeCallback and unregisterOnPageChangeCallback. So I used those. Ignore above code.

  • I only know java language for now but i will try this solution. – MattAzerty Aug 04 '22 at 11:09
  • 1
    @MattAzerty now you can see the soln. if this code works than please give me upvote or green tick. I am really starting out as SO answerer. Love from India. ❤️ – Shashank Daima Aug 04 '22 at 11:37
  • 1
    hey many thanks for yout time i appreciate it a lot ! it's working on the rotation screen !! But when i'm using the calendar to change month, it's still reset/clear my other fragments. It's because it don't go in "onSaveInstanceState" so there is no saving... – MattAzerty Aug 04 '22 at 12:36
  • Look I am not sure how experienced you are in android but what I did was `savedInstanceState` works like a hashmap who lifecycle is equal to time period a particular fragment remains in fragment backstack. So, when you change your orientation the previous fragment need to get cleared up and hence, before it calls `onDestroy`, it calls `onSaveInstanceState` were we saved only one parameter named `pagerPosition`. And whenever a new fragment onCreateView is called i am trying to get the previous value of `pagerPosition`. You can have as many such values as you need to restore fragment. – Shashank Daima Aug 04 '22 at 13:50
  • for your calendar thing what you can do is to change `Date` type variable to epoch and store that long value in new field just like i did with `pagerPosition` and `PAGER_POSITION_TAG`. You just need 2-3 Lines more. – Shashank Daima Aug 04 '22 at 13:53
  • i'm very new in android. It's not that i can't save the date or anything it's more when i update a fragment with keyboard or when switching month in calendar, the other fragments in the viewpager2 are "killed". I will try to learn more abour saving and restoring Fragment State but i have trouble to see where to start for my issue. anyway many thanks for your time. – MattAzerty Aug 04 '22 at 14:01
  • ok try to learn from codelabs from google on activity or fragment lifecycle. I learn mostly from there. check youtube videos. that can be helpful. – Shashank Daima Aug 04 '22 at 14:07
0

in the end this:

binding.activityMainViewpager.setOffscreenPageLimit(2);

have solved the rest of my issue ^^'

MattAzerty
  • 13
  • 1
  • 5