I read several posts ( http://tinyurl.com/pb8es74 , http://tinyurl.com/p9pcfcv ... ) About this, but cannot find a solution.
I have a MainActivity that loads a layout containing a ViewPager with three fragments (which have different layouts depending on orientation).
I added this function to the action so that it would not be destroyed when the orientation changed, as I may have some dialogs or open windows open:
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|uiMode"
Therefore, I rewrote the "onConfigurationChanged" method, which simply calls the loadLayout method, where everything related to the view has been done:
private void loadLayout() { setContentView(R.layout.layout_main_activity); pager = (ViewPager) this.findViewById(R.id.pager); pager.setPageTransformer(true, new DepthPageTransformer()); adapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); adapter.addFragment(Fragment1.getInstance()); adapter.addFragment(Fragment2.getInstance()); adapter.addFragment(Fragment3.getInstance()); pager.setAdapter(adapter); pager.setOffscreenPageLimit(2);
The code for the fragments is:
public class FragmentNature extends Fragment { private static FragmentNature instance; private SoundManager sM; private View rootView; Hashtable<String, ImageButton> h_Images; private Handler repeatUpdateHandler = new Handler(); private Toast volumeToast; public static FragmentNature getInstance() { if (instance == null) instance = new FragmentNature(); return instance; } public void nullInstance() { instance=null; } public FragmentNature(){ h_Images=new Hashtable<String,ImageButton>(); } public void onCreate(Bundle savedInstanceState) { Log.d("Naturing", "On create->Estoy en el fragment nature"); super.onActivityCreated(savedInstanceState); sM=SoundManager.getInstance(); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView=null; Log.d("Naturing", "On createView->Estoy en el fragment nature"); rootView = inflater.inflate(R.layout.fragment_nature, container, false); final ImageButton imgRain= (ImageButton) rootView.findViewById(R.id.n_rain); h_Images.put("n_rain",imgRain); imgRain.setImageResource(R.drawable.n_rainsel); final ImageButton imgRiver= (ImageButton) rootView.findViewById(R.id.n_river); imgRiver.setImageResource(R.drawable.n_riversel); final ImageButton imgWave= (ImageButton) rootView.findViewById(R.id.n_wave); h_Images.put("n_wave",imgWave); imgWave.setImageResource(R.drawable.n_wavesel); final ImageButton imgWind= (ImageButton) rootView.findViewById(R.id.n_wind); h_Images.put("n_wind", imgWind); imgWind.setImageResource(R.drawable.n_windsel); final ImageButton imgFire= (ImageButton) rootView.findViewById(R.id.n_fire); h_Images.put("n_fire", imgFire); imgFire.setImageResource(R.drawable.n_firesel); final ImageButton imgTree= (ImageButton) rootView.findViewById(R.id.n_tree); h_Images.put("n_tree", imgTree); imgTree.setImageResource(R.drawable.n_treesel); final ImageButton imgStorm= (ImageButton) rootView.findViewById(R.id.n_storm); h_Images.put("n_storm", imgStorm); imgStorm.setImageResource(R.drawable.n_stormsel); final ImageButton imgDrop= (ImageButton) rootView.findViewById(R.id.n_drop); h_Images.put("n_drop", imgDrop); imgDrop.setImageResource(R.drawable.n_dropsel); final ImageButton imgDeepSea= (ImageButton) rootView.findViewById(R.id.n_deepsea); h_Images.put("n_deepsea", imgDeepSea); imgDeepSea.setImageResource(R.drawable.n_deepseasel); return rootView; } private boolean isNetworkAvailable(){ Context context=ApplicationManager.getAppContext(); ConnectivityManager connectivityManager= (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo= connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } public void changeImageStatus(String sound, int imgId){ Log.d("Naturing", "FragmentNature::changeImageStatus | sound= " + sound + " imgId=" + imgId); h_Images.get(sound).setImageResource(imgId); }
}
The problem is that when you change the orientation, the mainActivity layout loads correctly, but the viewPager does not. In fact, it disappears, and nothing is visible in its place. In any case, the viewPager exists as I can go through it and reach the end (I know this because I can see the blue sign when you reach the end of the menu, etc.).
I also tried calling the OncreateView method for the fragments, but nothing changed.
Any idea? Thanks and kindly.
EDIT 1
If I select "pager.setOffscreenPageLimit (2);", after changing the orientation, fragments 1 and fragment 3 will be visible when I scroll through the viewer for a while, but fragment 2 does not appear in any case. I know that this is not so much, but in case it gives us a hint.
EDIT 2
If I struck out a line in the manifest, it works fine, as it calls onCreateView MainActivity and Fragment. But I need a line in the manifest.