I have 2 fragments.

The first fragment has 1 TextView and 1 button, the second fragment has 1 TextView , EditText and 1 button.

How to make so that when you press the buttons they alternate?

Ie, first the activity opens and the 1st fragment is displayed on it, when you press the button, the 2nd fragment appears (when you press the button), the 1st fragment opens again, and so we allow a certain number of times, let will be 10.

How to implement it?

    2 answers 2

    Recently, he asked himself a similar question .

    As a result, this method was written:

     public void showFragment(String fragmentName) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // Получаем список фрагментов, которые находятся в FragmentManager ArrayList<Fragment> existingFragments = (ArrayList<Fragment>) fragmentManager.getFragments(); // Фрагмент, который в данный момент отображен на экране Fragment shownFragment = null; if(existingFragments != null) { for(Fragment curFragment : existingFragments) { if(curFragment.isVisible()) { shownFragment = curFragment; break; } } } // Фрагмент, который необходимо отобразить на экране Fragment neededFragment = null; // Если в данный момент на экране не отображен ни один фрагмент, либо отображен, но не тот, который требуется показать if(shownFragment == null || !shownFragment.getClass().getSimpleName().equals(fragmentName)) { // Проверяем, есть ли фрагмент, который нужно отобразить, в FragmentManager if(shownFragment != null) neededFragment = fragmentManager.findFragmentByTag(fragmentName); // Если нужного фрагмента нет, то создаем его и добавляем в FragmentManager if(neededFragment == null) { switch(fragmentName) { case "FragmentOne": neededFragment = new FragmentOne(); fragmentTransaction.add(R.id.fragmentContainer, neededFragment, "FragmentOne"); break; case "FragmentTwo": neededFragment = new FragmentTwo(); fragmentTransaction.add(R.id.fragmentContainer, neededFragment, "FragmentTwo"); break; case "FragmentThree": neededFragment = new FragmentThree(); fragmentTransaction.add(R.id.fragmentContainer, neededFragment, "FragmentThree"); break; } } // Скрываем старый фрагмент if(shownFragment != null) fragmentTransaction.hide(shownFragment); // Показываем новый фрагмент fragmentTransaction.show(neededFragment); fragmentTransaction.commit(); } } 

      You need to realize intent, that is, intent .

      First, you give via intent in 2 activity and you produce your functions there. For a more detailed answer put the source I will write on it

      • intent of what? I have a fragment. Need a more detailed answer - Martinez Toni
      • what difference do you have a fragment or that an intent action for a more detailed answer needs a code, and then I did not quite understand your conditions, you cannot convey the essence - elik
      • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky