Choose which Fragment will be shown in Activity

Asked

Viewed 220 times

0

I have a Activity that the email is requested and if the email exists I show a Activity with a Fragment for entering the password. If this email n exists, I want to display a Fragment, in this same Activity, to make a record.

My question is: How to choose which Fragment will be inflated ? How can I do this?

  • Each fragment is represented by a distinct class and layout. No error. You might think of it as a Fragment being a piece of a screen that will temporarily superimpose Activity.

  • @Mr_anderson thank you, I will inflate them, in another Activity, in this case step to the Bundle of Activity the parameters and depending on what get I do the setcontentview to the correct Fragment , in the onCreate of Activity in question?

  • Voce can receive a boolean parameter in Activity #2, type temCadastro. Make an if in Activity nº2 --- if(temCadastro == true) {typeSenha();} Else {displayCadastro();} Inside typeSend and moostraCarting you inflate the fragment, not the xml layout of the fragment. Important to note that I find it more efficient to create a class for the fragment and not merely to change the layout.

  • The logical test if you have registered is done in Activity nº2 (the xml of this Activity will be "blank"). Activity decides which fragment to launch based on the temCadastro logic test.. The fragment will "sprout" on top of the Activity (the fragment consists of a specific class of fragment+xml). The interesting thing to note is that the fragment is different from a pop-up, known as dialog.

  • @Mr_anderson got it. Put as answer that mark. Thank you.

2 answers

2


You can receive a Boolean parameter in Activity #2, for example "temCadastro". Do an if in Activity #2

    if(temCadastro == true) {
      digitarSenha();
    } else { mostraCadastro();
    } 

Inside the type methodNew() and the displaCadastro() you inflate the fragment, not the xml layout of the fragment.
The logical test if you have registered is done in Activity nº2 (the xml of this Activity will be "blank"). Activity decides which fragment to launch based on the temCadastro logic test.. The fragment will "sprout" on top of the Activity (the fragment consists of a specific class of fragment+xml). The interesting thing to note is that the fragment is different from a pop-up, known as dialog.

2

First it is necessary to create a condition to verify your questions

If this email does not exist, I want to display a Fragment..

Try to create a method that returns a boolean with the name for example emailExiste () So you do like this:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if(emailExiste){        
    transaction.replace(R.id.fragment_container, fragmentEmailExiste);      
} else {        
    transaction.replace(R.id.fragment_container, fragmentEmailNaoExiste);  
}    
transaction.addToBackStack(null);
transaction.commit(); 
  • Opa valeu! All this checking is done in an Activity previous to the one that has these Fragments. Is it possible to set the correct Fragment, in this Activity that will go up, passing for example, the email by Bundle? So, the setContentView(R.layout.activity_new_login_fragment); should be alterado para o activity_new_empty_fragment .. . understood my question?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.