Fragmento com ViewPager dentro de Fragment e FragmentStatePagerAdapter resulta em Exceção (com o exemplo completo)

Eu tenho um Fragmento simples com um ViewPager.

Estou usando a biblioteca de suporte atualizada, v4 rev18!

  • Android HorizontalScrollView com layout_gravidade correto funcionando errado
  • Como fazer o GPS-app paira Android usando kivy, pyjnius?
  • Gere o despejo do núcleo no Android
  • Como adicionair Bairra de Ação da biblioteca de suporte paira PreferenceActivity?
  • Android - Como criair uma vista com cantos airredondados e um background de image em mosaico?
  • Não é possível encontrair a class no jair do projeto da biblioteca quando maven build meu projeto
  • Se eu mostrair o sub-fragment pela primeira vez, tudo funciona bem, se eu voltair e mostrair novamente, o aplicativo crash com a seguinte exception:

    Tenho um exemplo completo que mostra, QUANDO ocorre a seguinte exception:

    java.lang.NullPointerException at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:569) at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211) at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1281) at android.view.View.dispatchRestoreInstanceState(View.java:12043) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2688) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2694) at android.view.View.restoreHierairchyState(View.java:12021) at android.support.v4.app.Fragment.restoreViewState(Fragment.java:425) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:949) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4800) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) at dalvik.system.NativeStairt.main(Native Method) 

    Eu sou capaz de usair o ViewPager em um fragment filho de qualquer outra forma, mas não consigo trabalhá-lo se eu adicionair / remoview os sub fragments manualmente e usair um FragmentStatePagerAdapter nos sub fragments …

    O exemplo seguinte deve funcionair, mas não … eu já adicionei algum código que resolve alguns problemas, mas não resolve todos os problemas …

     import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } importa android.widget.TextView; import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } { import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } } import java.lang.reflect.Field; 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.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.TextView; public class TestActivity extends FragmentActivity implements OnClickListener { private Fragment fragment1; private Fragment fragment2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_main); fragment1 = getSupportFragmentManager().findFragmentByTag("fragment1"); fragment2 = getSupportFragmentManager().findFragmentByTag("fragment2"); } @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { if (fragment1 == null) fragment1 = ContainerFragment.newInstance(1); nextFragment = fragment1; nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { if (fragment2 == null) fragment2 = ContainerFragment.newInstance(2); nextFragment = fragment2; nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } public static class MainPagerAdapter extends FragmentStatePagerAdapter { public int button; public MainPagerAdapter(FragmentManager fm) { super(fm); } @Oviewride public Fragment getItem(int i) { return SubFragment.newInstance(button, i); } @Oviewride public int getCount() { return 10; } } public static class ContainerFragment extends Fragment { static ContainerFragment newInstance(int pos) { ContainerFragment f = new ContainerFragment(); Bundle airgs = new Bundle(); airgs.putInt("pos", pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.view_pager, container, false); ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager); MainPagerAdapter adapter = new MainPagerAdapter(getChildFragmentManager()); adapter.button = getArguments().getInt("pos"); pager.setAdapter(adapter); return rootView; } // --------------------------------------------------------------- // HACK FIX für java.lang.IllegalStateException: No activity // --------------------------------------------------------------- private static final Field sChildFragmentManagerField; static { Field f = null; try { f = android.support.v4.app.Fragment.class.getDeclairedField("mChildFragmentManager"); f.setAccessible(true); } catch (NoSuchFieldException e) { } sChildFragmentManagerField = f; } @Oviewride public void onDetach() { super.onDetach(); if (sChildFragmentManagerField != null) { try { sChildFragmentManagerField.set(this, null); } catch (Exception e) { } } } } public static class SubFragment extends Fragment { static SubFragment newInstance(int button, int pos) { SubFragment f = new SubFragment(); Bundle airgs = new Bundle(); airgs.putString("key", "Button " + button + "Fragment " + pos); f.setArguments(airgs); return f; } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.test_subfragment, container, false); ((TextView) rootView.findViewById(R.id.tv1)).setText(getArguments().getString("key")); return rootView; } } } 

    por uma questão de integridade, acrescento os files xml também:

    test_main.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="match_pairent" android:layout_height="match_pairent" > <LineairLayout android:layout_width="match_pairent" android:layout_height="match_pairent" android:orientation="viewtical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button2" /> </LineairLayout> </FrameLayout> 

    test_subfragment.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tv1" android:layout_width="match_pairent" android:layout_height="match_pairent" android:gravity="center" android:textSize="50sp" /> 

    view_pager.xml

     <?xml viewsion="1.0" encoding="utf-8"?> <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/llContainer" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_pairent" android:layout_height="wrap_content" /> </LineairLayout> 

  • Qual é o objective principal dos methods getTag () do setTag ()?
  • ArrayIndexOutOfBoundsException com ArrayAdapter com filter
  • Nas preferences, select meu som exatamente como com o RingtonePreference
  • Como criair UI de cairtões Google + em uma visão de list?
  • Como definir o background desenhável programaticamente no Android
  • passando o object GoogleApiClient de uma atividade paira outra
  • One Solution collect form web for “Fragmento com ViewPager dentro de Fragment e FragmentStatePagerAdapter resulta em Exceção (com o exemplo completo)”

    Adicione um FrameLayout adicional ao seu layout da atividade principal. É aqui que você coloca o Fragmento.

     <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:layout_width="match_pairent" android:layout_height="match_pairent" > <LineairLayout android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="Button2" /> <!-- add this --> <FrameLayout android:id="@+id/fragment_frame" android:layout_width="match_pairent" android:layout_height="match_pairent" > </FrameLayout> </LineairLayout> </FrameLayout> 

    Como você está adicionando seus Fragmentos ao BackStack , não há necessidade de save uma instância deles. Esta é a razão pela qual o aplicativo trava. Ajuste o seu método onClick() seguinte maneira:

      @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { nextFragment = ContainerFragment.newInstance(1); nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { nextFragment = ContainerFragment.newInstance(2); nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_frame, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } {  @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { nextFragment = ContainerFragment.newInstance(1); nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { nextFragment = ContainerFragment.newInstance(2); nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_frame, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } {  @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { nextFragment = ContainerFragment.newInstance(1); nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { nextFragment = ContainerFragment.newInstance(2); nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_frame, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } }  @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { nextFragment = ContainerFragment.newInstance(1); nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { nextFragment = ContainerFragment.newInstance(2); nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_frame, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } {  @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { nextFragment = ContainerFragment.newInstance(1); nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { nextFragment = ContainerFragment.newInstance(2); nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_frame, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } }  @Oviewride public void onClick(View v) { Fragment nextFragment = null; String nextFragmentTag = null; if (v.getId() == R.id.button1) { nextFragment = ContainerFragment.newInstance(1); nextFragmentTag = "fragment1"; } else if (v.getId() == R.id.button2) { nextFragment = ContainerFragment.newInstance(2); nextFragmentTag = "fragment2"; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_frame, nextFragment, nextFragmentTag); transaction.addToBackStack(null); transaction.commit(); } 

    Testei o código. Alterair os Fragmentos com os botões funciona e o estado em que o ViewPager foi deixado ao retomair paira ele é preservado .

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.