0

I try to persist my UI after rotating the screen. I used onSaveInstanceState() to track the current tab position. Everything is working fine. The currentTabPosition has the correct value.

But this portion of code is not working:

viewPager2.post(() -> {        
    viewPager2.setCurrentItem(currentTabPosition, false);
    tabLayout.getTabAt(currentTabPosition);
});

If I give the value manually like instead of currentTabPosition I give 1, the correct tab is always showing up.

Here is the full code:

public class ConfigureFragment extends Fragment {

    private TabLayout tabLayout;
    private ViewPager2  viewPager2;
    private ConfigureViewPageAdapter configureViewPageAdapter;

    private int currentTabPosition = 0; // Track the current selected tab position
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.fragment_configure, container, false);

       tabLayout = fragmentView.findViewById(R.id.tabLayoutInConfigure);
       viewPager2 = fragmentView.findViewById(R.id.viewPagerInConfigure);

       configureViewPageAdapter = new ConfigureViewPageAdapter(requireActivity());

       viewPager2.setAdapter(configureViewPageAdapter);

       String[] titles = new String[]{"Activity","Sensors"};



       new TabLayoutMediator(tabLayout,viewPager2,((tab, position) -> {tab.setText(titles[position]);})).attach();

        if(savedInstanceState!=null){
            currentTabPosition = savedInstanceState.getInt("currentTabPosition");
            Log.d("ADDD",currentTabPosition+"inside");
        }

        Log.d("ADDD",currentTabPosition+"Outside");

        viewPager2.post(() -> {
            viewPager2.setCurrentItem(currentTabPosition, false);
            tabLayout.getTabAt(currentTabPosition);
        });


        // Inflate the layout for this fragment
        return fragmentView;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save the current selected tab position
        outState.putInt("currentTabPosition", tabLayout.getSelectedTabPosition());
    }

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

}

And the related adapter

package com.hasanur.realtimehar.Adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.hasanur.realtimehar.ActivityConfigureFragment;
import com.hasanur.realtimehar.SensorConfigureFragment;

import java.util.ArrayList;

public class ConfigureViewPageAdapter extends FragmentStateAdapter {

    private ArrayList<Fragment> fragmentArrayList = new ArrayList<Fragment>();

    public ConfigureViewPageAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
        add(new ActivityConfigureFragment());
        add(new SensorConfigureFragment());


    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
       /* switch (position){
            case 0 :
                return new ActivityConfigureFragment();
            case 1:
                return new SensorConfigureFragment();
        }*/
       // return new ActivityConfigureFragment();
        return fragmentArrayList.get(position);
    }


    @Override
    public int getItemCount() {
        return fragmentArrayList.size();
    }

    public void add(Fragment fragment){
        fragmentArrayList.add(fragment);
    }
}

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • Edit your title and question to point out the problem well. Saying _**this code is not working**_ is not enough for us to catch bugs. Try to describe your problem well. – Kozmotronik Jul 08 '23 at 06:58
  • No One is giving the ans. yet throwing comment to edit the post. Ironic. – Hasan Seam Jul 11 '23 at 09:44

0 Answers0