Guys, how to make the launch of an application in Activity have one Fragment, and when you press the button another Fragment appears, while the 1st is deleted.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

To dynamically change fragments use the replace() method.

For example:

 ExampleFragment fragment = new ExampleFragment(); // Фрагмент, которым собираетесь заменить первый фрагмент FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Или getSupportFragmentManager(), если используете support.v4 transaction.replace(R.id.fragment_container, fragment); // Заменяете вторым фрагментом. Т.е. вместо метода `add()`, используете метод `replace()` transaction.addToBackStack(null); // Добавляете в backstack, чтобы можно было вернутся обратно transaction.commit(); // Коммитете 

More details can be found in the official documentation: One , Two .

And also look at this lesson .

    At startup, add one piece. When you click on the button add another piece. You can even put both fragments in the same code:

     Fragment newFragment = new YourFragment(); //YourFragment заменить на нужный FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, newFragment); //fragment_container заменить на Ваш id контейнера transaction.commit();