Reputation: 1206
I have a viewpager that hosts a fragment that in turn host two nested fragments. They are initialized just fine but when i rotate the screen the last of the nested fragments disappears (Page2Fragment). When I rotate back again it's there.
I think it might be related to this issue:
https://code.google.com/p/android/issues/detail?id=52112
But that issue is said to be fixed but it doesn't work for me. My question is if anyone has a workaround for this? I post the code I use below:
ViewPagerActivity:
package com.example.swekjaks.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class ViewPagerActivity extends FragmentActivity {
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.task_pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return new MainContainerFragment();
}
@Override
public int getCount() {
return 1;
}
}
}
MainContainerFragment:
package com.example.swekjaks.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainContainerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_main_container, container, false);
FragmentManager fragmentManager = getChildFragmentManager();
AddFirstFragment(fragmentManager);
AddNextFragment(fragmentManager);
return rootView;
}
private void AddFirstFragment(FragmentManager fragmentManager) {
Page1Fragment fragment = (Page1Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment1);
if (fragment == null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Page1Fragment page1Fragment = new Page1Fragment();
fragmentTransaction.add(R.id.frameLayout_fragment1, page1Fragment);
fragmentTransaction.commit();
} else {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout_fragment1, fragment);
fragmentTransaction.commit();
}
}
private void AddNextFragment(FragmentManager fragmentManager) {
Page2Fragment fragment = (Page2Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment2);
if (fragment == null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Page2Fragment page2Fragment = new Page2Fragment();
fragmentTransaction.add(R.id.frameLayout_fragment2, page2Fragment);
fragmentTransaction.commit();
} else {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayout_fragment2, fragment);
fragmentTransaction.commit();
}
}
}
View for FragmentMainContainer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:orientation="vertical"
android:id="@+id/frameLayout_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:orientation="vertical"
android:id="@+id/frameLayout_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
Page1Fragment:
package com.example.swekjaks.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_page_1, container, false);
return rootView;
}
}
Page2Fragment:
package com.example.swekjaks.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Page2Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_page_2, container, false);
return rootView;
}
}
Upvotes: 0
Views: 1632
Reputation: 1206
I solved this finally by adding the following piece of code:
@Override
public void onSaveInstanceState(Bundle outState) {
//Remove fragments:
FragmentManager fragmentManager = getChildFragmentManager();
Page1Fragment fragment1 = (Page1Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment1);
Page2Fragment fragment2 = (Page2Fragment) fragmentManager.findFragmentById(R.id.frameLayout_fragment2);
if (fragment1 != null){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.detach(fragment1);
fragmentTransaction.commit();
}
if (fragment2 != null){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.detach(fragment2);
fragmentTransaction.commit();
}
super.onSaveInstanceState(outState);
}
So, I detach the fragments and instead of calling fragmentTransaction.replace(...) I call
fragmentTransaction.attach(fragment);
This seems to work and so far I haven't seen any drawbacks
Upvotes: 2