0

I'm creating an Activity with ViewPager that using FragmentStatePagerAdapter. But when I'm trying to rotate my phone, it crash and come with error "Fragment has not been attached yet"

Here's my adapter:

public class SalesPagingAdapter extends FragmentStatePagerAdapter {

private CharSequence[] titles;
private int tabCount;
private List<Fragment> fragmentList = new ArrayList<>();

public SalesPagingAdapter(Context ctx, FragmentManager fm, CharSequence[] titles, int tabCount) {
    super(fm);

    this.titles = titles;
    this.tabCount = tabCount;

    for (int x = 0 ; x < tabCount ; x++) {
        if (x == 0) fragmentList.add(new BaseFragment(new DashboardSales()));
        else fragmentList.add(new BaseFragment());
    }
}

@Override
public Fragment getItem(int pos) {
    return fragmentList.get(pos);
}

@Override
public CharSequence getPageTitle(int position) {
    return titles[position];
}

@Override
public int getCount() {
    return tabCount;
}

public void SetTab(int idx, Fragment frag) {
    BaseFragment basicFrag = (BaseFragment)fragmentList.get(idx);
    basicFrag.getChildFragmentManager().beginTransaction().replace(R.id.container, frag).commit();
}

public void SetLoadTab(int idx, Fragment frag) {
    BaseFragment basicFrag = (BaseFragment)fragmentList.get(idx);
    basicFrag.SetLoadFragment(frag);
}

public FragmentControl GetFragment(int idx) {
    return (FragmentControl)fragmentList.get(idx).getChildFragmentManager().findFragmentById(R.id.container);
}}

In my activity, onCreate, I have called salesPagingAdapter = new SalesPagingAdapter(PreMainActivitySales.this, getSupportFragmentManager(), tabTitles, tabCount); viewPager.setAdapter(salesPagingAdapter); viewPager.setOffscreenPageLimit(1);

And then when I swipe the ViewPager, I call int i = viewPager.getCurrentItem(); FragmentControl baseFrag = salesPagingAdapter.GetFragment(i);

If I rotate the phone without swipe the ViewPager, it'll be fine. But when I rotate it, even get back to previous orientation, when I swipe, it'll crash and give "Fragment has not been attached yet" error.

JNTX 1412
  • 65
  • 13

2 Answers2

0

In your AndroidManifest.xml of your activity, when you define a new activity you must writeandroid:screenOrientation="unspecified":

<activity
    android:screenOrientation="unspecified"
    android:name="com.example.MainActivity"
    android:label="@string/app_name" >

Or write nothing, because unspecified is the default value.

If you have to change orientation programmatically, you must write this:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); Or

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Jackey kabra
  • 199
  • 1
  • 9
  • I try it, the error has gone, but my landscape layout is not called when the orientation changed. When I did not handle the orientation at my manifest, the layout will show according to the orientation, but if I handle it, the layout won't change. – JNTX 1412 Nov 28 '17 at 06:26
  • Okay, You will make one folder layout-land in res and paste you xml file than run it. i think it will work. – Jackey kabra Nov 28 '17 at 06:49
  • Already done it. I have layout and layout-land. When I rotate my phone, It change to layout-land, but when I swipe it, I got "Fragment has not been attached yet" error. If I handle the orientation in my manifest(android:configChanges), when I rotate the phone, the error is gone, but the layout won't go to layout-land. – JNTX 1412 Nov 28 '17 at 07:13
0
Try this type code four you fragment class like onAttach() and on onDetach().

public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2;
protected FragmentActivity mActivity;
MyAdapter adapter;
View x;
Offers offersFragment;

public TabFragment() {
    // Required empty public constructor
}

public void onBackPressed() {
   // do stuff
    viewPager.setCurrentItem(0, true);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = (FragmentActivity) activity;
}


public void navigateFragment(int position){

    viewPager.setCurrentItem(position, true);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    x = inflater.inflate(R.layout.content_main_menu, null);
    tabLayout = x.findViewById(R.id.tabs);
    viewPager = x.findViewById(R.id.viewpager);
    setupViewPager(viewPager);
    //viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
    viewPager.setOffscreenPageLimit(1);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return x;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onDetach() {
    super.onDetach();

    try {
        Field childFragmentManager = 
Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}


private void setupViewPager(ViewPager viewPager) {
    adapter = new MyAdapter(getChildFragmentManager());
    adapter.addFragment(new HomeFragment(), "HOME");
    adapter.addFragment(new Offers(), "OFFERS");

    viewPager.setAdapter(adapter);


}
Jackey kabra
  • 199
  • 1
  • 9