j'ai un problème avec des fragments lorsque mon appareil est Alterner: i ont 5 fragments:Tous les fragments visibles après la rotation
- Menu est le premier fragment, lorsque le démarrage de l'activité, il est lancé.
- AddUser (fragment simple que je peux voir quand je presse un bouton dans le fragment de menu)
- DetailApp (fragment simple que je peux voir quand je presse un bouton dans le fragment de menu)
- DetailUser (fragment simple que je peux voir quand je presse un bouton dans le fragment de menu)
- LISTUSER (ListFragment que je peux voir quand je presse un bouton dans le fragment de menu)
quand je commence mon activité en mode vertical, je peux voir correctement mes fragments , quand je fais tourner mon smartphone, je peux voir .. tous les fragments se chevauchent! si je lance LISTUSER j'ai:
IllegalArgumentException: No view found for id 0x... for fragment ...
quand commencer mes fragments de menu:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager);
c'est oncreate
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
//first frame show a menu
Menu firstFragment = new Menu();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
conteneur fragment au début est vide ..
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
quand je presse un bouton et l'un des fragments commencent:
@Override
public void onDetailApp(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Capture the article fragment from the activity layout
DetailApp appFrag = (DetailApp)
getSupportFragmentManager().findFragmentById(R.id.detailapp_fragment);
if (appFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
appFrag.updateAppointmentView(position);
} else {
// If the frag is not available, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
DetailApp newFragment = new DetailApp();
Bundle args = new Bundle();
args.putInt(DetailApp.ARG_APPOINTMENT, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
c'est onCreateView de DetailUser, AddUser, DetailApp:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.detail_adduser, container, false);
}
Qui peut me aider?
pas de changement .. j'utilise ajouter seulement à onCreate .. – fabio
mais pourquoi je peux voir tous les fragments si personne ne l'appelle? – fabio
si vous arrêtez votre code avant qu'il ne retourne sur cette ligne 'if (savedInstanceState! = Null) {' et vérifiez le contenu (perspective Debug), vous verrez que tout l'état des fragments est stocké là et le système est en train de recréer les dans le super.onCrée(). Donc, vous ne l'appelez pas, mais le système est. – Budius