2013-01-11 4 views
2

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

  1. Menu est le premier fragment, lorsque le démarrage de l'activité, il est lancé.
  2. AddUser (fragment simple que je peux voir quand je presse un bouton dans le fragment de menu)
  3. DetailApp (fragment simple que je peux voir quand je presse un bouton dans le fragment de menu)
  4. DetailUser (fragment simple que je peux voir quand je presse un bouton dans le fragment de menu)
  5. 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?

Répondre

-1

remplacer

getSupportFragmentManager().beginTransaction() 
.add(R.id.fragment_container, firstFragment).commit(); 

par

getSupportFragmentManager().beginTransaction() 
.replace(R.id.fragment_container, firstFragment).commit(); 

99% des temps il résout les problèmes de duplication.

+0

pas de changement .. j'utilise ajouter seulement à onCreate .. – fabio

+0

mais pourquoi je peux voir tous les fragments si personne ne l'appelle? – fabio

+0

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

-1

-je ajouter sur mon activité manifeste et passer outre onConfigurationChanged: android: configChanges = "keyboardHidden | orientation | ScreenSize"

<activity 
     android:name="com.map.manager.ManagerActivity" 
     android:configChanges="keyboardHidden|orientation|screenSize" 
     android:label="@string/title_activity_manager" > 
    </activity> 

et j'ai pas plus de problèmes! Je peux voir correctement la disposition verticale et horizontale (quand je fais tourner le smartphone). mais j'ai trouvé ceci: why-not-use-always-androidconfigchanges dois-je mettre dans "blacklist" tous les changements de configuration possibles? écran tactile écranSize etc?

+0

Vous n'avez pas affaire au problème, vous le couvrez simplement. Le bon moyen serait de gérer les changements de configuration, ne pas essayer de les supprimer. Par expérience, je peux dire: Nous avions des téléphones qui ignoraient totalement les paramètres de configuration, donc traiter les changements de configuration était la seule chance de maintenir un comportement cohérent sur différents téléphones. – AgentKnopf

Questions connexes