Exchange Fragments Android

Asked

Viewed 1,170 times

1

Good people, I’m starting with Android Studio and I’m developing a small app and I made a menu, and I would like when I click on it, open another Fragment. I’ve tried several things already and nothing :/

The last was this

FragmentManager fragmentManager = getFragmentManager();

    if (id == R.id.Aries)
    {
      fragmentManager.beginTransaction().replace(R.id.content_frame, new Aries())
         .commit();
    }

But where is the "new Aries got the error of "cannot find Symbol fragment_aries"

My app will be horoscope, and then I will connect to online database.

Any help on that part too, thank you

Thank you

Attt

Lucas Soares

MainActivity Quando abro o fragment

  • You can add codes from your class Aries ?

  • I had mistakes, but I followed the step by step of the friend’s answer and solved everything. And now I have this problem of overwriting Fragment ps: I can’t see the response from the user who helped me

3 answers

1

I do as follows: First I will instantiate a Switch for the menu items clicked.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //OUTRAS IMPLEMENTAÇÕES

    setContentView(R.layout.activity_main);mNavigationView = (NavigationView)findViewById(R.id.navigation_view);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            selecionarOpcaoMenu(menuItem);
            return true;
        }
    });
}

Then I implement the method that handles the click event.

private void selecionarOpcaoMenu(MenuItem menuItem){
    mOpcaoSelecionada = menuItem.getItemId();
    menuItem.setChecked(true);
    mDrawerLayout.closeDrawers();

    String titulo = menuItem.getTitle().toString();

    FragmentManager fm = getSupportFragmentManager();

    if(fm.findFragmentByTag(titulo) == null){
        Fragment primeiroNivelFragment = new Fragment();

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.conteudo, primeiroNivelFragment, titulo)
                .commit();
    }
}

I hope I helped you, I have an example of navigation with Fragments if you want to take a look. Here

1

To make the transition between Mainactivity Fragments with Navdrawer, I use it like this:

   public void displayView(int viewId) {

        Fragment fragment = null;

        switch (viewId) {
            case R.id.fragment_1:
                fragment = new Fragment1();
                title  = "Fragment 1";
                break;
            case R.id.fragment_2:
                fragment = new Fragment2();
                title  = "Fragment 2";
                break;

        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, fragment);
            ft.commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

    }

I hope I helped. You can see more here.

0

For a new Activity (Activity):

Create a new Activity by the name of MainActivity2 and insert this code below into your method onNavigationItemSelected of his MainActivity:

Intent intent = new Intent(this, MainActivity2.class); 
startActivity(intent);

For a new Fragment (Fragment):

Create a new Fragment by the name of sobreFragment and insert this code below into your method onNavigationItemSelected of his MainActivity:

sobreFragment fragment = new sobreFragment();
android.support.v4.app.FragmentTransaction fragmentTrasaction =
getSupportFragmentManager().beginTransaction();
fragmentTrasaction.replace(R.id.fragment_container, fragment);
fragmentTrasaction.commit();
  • Thank you very much, I followed everything and managed to open Ragment, but it is opening and showing the contents of my Mainactivity below?

Browser other questions tagged

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