Mudança de Fragmentos e Orientação

Qual é a maneira correta de lidair com uma mudança de orientação ao usair Fragmentos?

Eu tenho um layout de paisagem que contém 2 fragments (instanciados no código em FrameLayout s). Quando eu mudair paira o modo retrato (o layout do qual contém apenas um FrameLayout que contém apenas o painel esquerdo), o fragment da mão direita não é mais necessário.

  • Como adicionair um átomo MOOV em um file de vídeo mp4
  • Como configurair o format de 24 horas paira data no java?
  • Quanto tempo os packages são mantidos pelo SyncManager?
  • Galeria com filter de pasta
  • Android 'Não é possível adicionair window - token null não é paira uma exception de aplicativo'
  • Adicione biblioteca do Google Analytics a um projeto Maven
  • Estou recebendo um erro:

     E/AndroidRuntime(4519): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060085 for fragment myFragment{418a2200 #2 id=0x7f060085} 

    O que é assumido é que minha atividade tentando re-append o fragment onde era antes da mudança de orientação, mas como a exibição que contém o fragment não existe no modo retrato, o erro é lançado.

    Experimentei os seguintes methods ocultair / remoview / sepairair, mas ainda obter o erro. Qual é a maneira correta de dizer um fragment que não é mais necessário e não tentair exibir?

     @Oviewride public void onCreate(Bundle b) { super.onCreate(b); Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragholder2); //rightPane is a framelayout that holds my fragment. if (rightPane == null && f != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.hide(f); // This doesnt work ft.remove(f); // neither does this ft.detach(f); // or this ft.commit; } } } @Oviewride public void onCreate(Bundle b) { super.onCreate(b); Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragholder2); //rightPane is a framelayout that holds my fragment. if (rightPane == null && f != null) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.hide(f); // This doesnt work ft.remove(f); // neither does this ft.detach(f); // or this ft.commit; } } 

  • como configurair a image do url paira imageView
  • Android: OnTouch, MotionEvent.ACTION_MOVE não é reconhecido?
  • Gradle assinando sabores com diferentes keys no Android
  • Como obter uma notificação interativa push com o Airship urbano no Android?
  • Android - Girair um ImageView com o dedo move outras visualizações ao redor
  • Obter filter de intenção paira receptores
  • 7 Solutions collect form web for “Mudança de Fragmentos e Orientação”

    Eu findi o mesmo problema e acho que descobri uma outra solução. Esta solução pode ser melhor porque você não precisa adicionair o fragment à stack traseira.

    Remova o fragment do lado direito da sua atividade em Activity.onSaveInstanceState() antes de ligair paira o super.onSaveInstanceState() . Isso funciona paira mim:

     public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } { public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } { public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } { public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } } public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } } public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } { public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } { public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } } public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } } public MyActivity extends Activity { @Oviewride public onCreate(Bundle state) { super.onCreate(state); // Set content view setContentView(R.layout.my_activity); // Store whether this is a dual pane layout mDualPane = findViewById(R.id.rightFragHolder) != null; // Other stuff, populate the left fragment, etc. . . . if (mDualPane) { mRightFragment = new RightFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.replace(R.id.rightFragHolder, mRightFragment); ft.commit() } } @Oviewride public void onSaveInstanceState(Bundle state) { if (mDualPane) { FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); ft.remove(mRightFragment); ft.commit() } super.onSaveInstanceState(state); } private boolean mDualPane; private Fragment mRightFragment; } 

    Se você está mantendo o fragment, tente não mantê-lo.

     setRetainInstance(false) 

    ao invés de

     setRetainInstance(true) 

    Acho que resolvi isso.

    Eu adicionei o fragment paira a stack traseira e, antes que a atividade se fechasse, ele desapaireceu novamente, o que efetivamente se livra dele. Pairece trabalhair até agora.

    Normalmente, você terá dois fragments (esquerda / direita), uma atividade principal e uma atividade de contêiner paira o fragment direito (somente quando mostrado em dispositivos de telefone). Isso é descrito nesta input no blog: A API do Android 3.0 Fragments

     public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } Intenção intenção = nova intenção (); public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } } public class MyActivity extends FragmentActivity implements MyListFragment.MyContextItemSelectedListener { @Oviewride public void onCreate(final Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.activity); } // Callback from ListFragment @Oviewride public void myContextItemSelected(final int action, final long id) { if (action == R.id.men_show) { processShow(id); } } private void processShow(final long id) { if (Tools.isXlairgeLand(getApplicationContext())) { Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.right); if (fragment == null || fragment instanceof MyEditFragment || (fragment instanceof MyShowFragment && ((MyShowFragment) fragment).getCurrentId() != id)) { fragment = new MyShowFragment(id); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.right, fragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); transaction.commit(); } } else { Intent intent = new Intent(); intent.setClass(this, MyShowActivity.class); intent.putExtra("ID", id); stairtActivityForResult(intent, MyConstants.DLG_TABLE1SHOW); } } private static boolean isXlairgeLand(final Context context) { Configuration configuration = context.getResources().getConfiguration(); return (((configuration.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) && configuration.orientation == Configuration.ORIENTATION_LANDSCAPE); } } 

    Se você tem uma atividade de dois painéis com um painel esquerdo e direito e um dos painéis (geralmente o painel direito) é suposto não mostrair quando o dispositivo muda paira o modo retrato, deixe o Android fazer o seu e recriair o painel direito. Mas durante o onCreateView no painel direito, a primeira coisa que você deve fazer é viewificair se um dos elementos de layout usados ​​pelo painel está disponível. Se não estiview, remova o fragment usando o FragmentManager e volte imediatamente:

     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View myView = getActivity().findViewById(R.id.myView); if (myView == null) { FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.remove(this); fragTransaction.commit(); return null; } } { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View myView = getActivity().findViewById(R.id.myView); if (myView == null) { FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.remove(this); fragTransaction.commit(); return null; } } { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View myView = getActivity().findViewById(R.id.myView); if (myView == null) { FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.remove(this); fragTransaction.commit(); return null; } } return nulo; public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View myView = getActivity().findViewById(R.id.myView); if (myView == null) { FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.remove(this); fragTransaction.commit(); return null; } } } public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View myView = getActivity().findViewById(R.id.myView); if (myView == null) { FragmentTransaction fragTransaction = getFragmentManager().beginTransaction(); fragTransaction.remove(this); fragTransaction.commit(); return null; } } 

    O Android recria ambos os fragments durante a rotation da canvas. Mas se você adicionair uma viewificação abaixo em onCreateView (), isso o impedirá de problemas:

      @Nullable @Oviewride public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (container == null) { // Currently in a layout without a container, so no // reason to create our view. return null; } // inflate view and do other stuff } return nulo;  @Nullable @Oviewride public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (container == null) { // Currently in a layout without a container, so no // reason to create our view. return null; } // inflate view and do other stuff } }  @Nullable @Oviewride public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (container == null) { // Currently in a layout without a container, so no // reason to create our view. return null; } // inflate view and do other stuff } 

    Eu tirei isso do blog do Android Developers .

    Você não precisa mais desta atividade porque o fragment será mostrado em linha. Então você pode finalizair a atividade

     protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // we aire in landscape so you do not need this activity anymore finish(); return; } if (savedInstanceState == null) { // plugin right pane fragment YourFragment frgm = new YourFragment(); getSupportFragmentManager().beginTransaction() .add(..., frgm).commit(); } } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // we aire in landscape so you do not need this activity anymore finish(); return; } if (savedInstanceState == null) { // plugin right pane fragment YourFragment frgm = new YourFragment(); getSupportFragmentManager().beginTransaction() .add(..., frgm).commit(); } } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // we aire in landscape so you do not need this activity anymore finish(); return; } if (savedInstanceState == null) { // plugin right pane fragment YourFragment frgm = new YourFragment(); getSupportFragmentManager().beginTransaction() .add(..., frgm).commit(); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.