There is a class MainActivity

public class MainActivity extends FragmentActivity { final int DIALOG_EXIT = 1; FragmentTransaction ft; FirstFragment ff = new FirstFragment(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ff = new FirstFragment(); getSupportFragmentManager().beginTransaction().add(R.id.frame, ff) .commit(); } @Override public void onBackPressed() { // TODO Auto-generated method stub if (getSupportFragmentManager().getBackStackEntryCount() > 0) { super.onBackPressed(); } else { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("EXIT").setMessage("Π£Π²Π΅Ρ€Π΅Π½ Ρ‡Ρ‚ΠΎ Ρ…ΠΎΡ‡Π΅ΡˆΡŒ Π²Ρ‹ΠΉΡ‚ΠΈ?^^").setPositiveButton("Π”Π°", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }).setNegativeButton("НСт", null).show(); } }} 

And also three identical fragments

 public class FirstFragment extends Fragment { FragmentTransaction ft; SecondFragment sf; ThirdFragment tf; Button next1; Button back1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.first, null); sf = new SecondFragment(); tf = new ThirdFragment(); Button next1 = (Button) v.findViewById(R.id.next1); Button back1 = (Button) v.findViewById(R.id.back1); next1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ft = getFragmentManager().beginTransaction(); switch (v.getId()) { case R.id.next1: ft.replace(R.id.frame, sf).addToBackStack(null); break; default: break; } ft.commit(); } }); back1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ft = getFragmentManager().beginTransaction(); switch (v.getId()) { case R.id.back1: //((Button)getActivity().findViewById(R.id.back1)); //getActivity() //(MainActivity()getActivity()). ft.replace(R.id.frame, tf).addToBackStack(null); break; default: break; } ft.commit(); } }); return v; }} 

Fragments are switched inside of themselves, how to make switching through ((MainActivity) getActivity ())?

    1 answer 1

    I advise you to do it through callback.
    those. describe the interface of the fragment

     public interface DoSomesingListener { public void doSomesing(); } 

    and you make a private field for a fragment (for all fragments) or for one basic fragment and inherit all fragments from it. with inheritance is preferable, because there will be no repetition of the code

     private DoSomesingListener doSomesingListener; 

    and setter to him

     public void setDoSomesingListener(final DoSomesingListener doSomesingListener) { this.doSomesingListener = doSomesingListener; } 

    Then, in the necessary activity, implement this interface and send to the fragments where these actions are to be performed, via setDoSomesingListener

    I hope clearly described his thought.

    But you can and as you suggested, but I would not do that.

    UPD: it turned out pretty massive, I advise you to compare written above, with the code below

     public class MainActivity extends FragmentActivity implements BaseFragment.OnFragmentChangeListener { private OneFragment oneFragment; private TwoFragment twoFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //ΠΏΡ€Π΅Π΄Π²Π°Ρ€ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹ΠΉ oneFragment ΠΈ twoFragment //Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ //Π΄Π°Π»Π΅Π΅ посылаСм ΡΠ»ΡƒΡˆΠ°Ρ‚Π΅Π»Ρ Π² ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ oneFragment.setOnFragmentChangeListener(this); twoFragment.setOnFragmentChangeListener(this); } @Override public void onFragmentChange() { //Ρ‚ΡƒΡ‚ происходит ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Ρ„Ρ€Π°Π³ΠΌΠ΅Π½Ρ‚ΠΎΠ², //Π»ΠΎΠ³ΠΈΠΊΡƒ ΡƒΠΆ Π½Π΅ знаю, придСтся Π΄Π΅Π»Π°Ρ‚ΡŒ самому } } public class BaseFragment extends Fragment { //Ρ‚ΡƒΡ‚ происходит всякая инициализация ΠΈ Π΄Ρ€ΡƒΠ³ΠΎΠΉ ΠΊΠΎΠ΄ public interface OnFragmentChangeListener { //ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎ ΠΌΠΎΠΆΠ΅Ρ‚ ΠΈΠΌΠ΅Ρ‚ΡŒ Π²Ρ…ΠΎΠ΄Π½Ρ‹Π΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹(Ρ‚Π°ΠΊ, для справки) public void onFragmentChange(); } protected OnFragmentChangeListener onFragmentChangeListener; public void setOnFragmentChangeListener(final OnFragmentChangeListener onFragmentChangeListener) { this.onFragmentChangeListener = onFragmentChangeListener; } //этот ΠΌΠ΅Ρ‚ΠΎΠ΄ Π½Π΅ обязатСлСн, Ρ‚Π°ΠΊ ΠΊΠ°ΠΊ ΠΏΠΎΠ»Π΅ onFragmentChangeListener с ΠΌΠΎΠ΄ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ΠΎΠΌ protected, // Π° Π·Π½Π°Ρ‡ΠΈΡ‚ всС наслСдники класса ΠΈΠΌΠ΅ΡŽΡ‚ ΠΊ Π½Π΅ΠΌΡƒ доступ // написал Π½Π° всякий случай public OnFragmentChangeListener getOnFragmentChangeListener() { return this.onFragmentChangeListener } } public class OneFragment extends BaseFragment { //Ρ‚ΡƒΡ‚ происходит всякая инициализация ΠΈ Π΄Ρ€ΡƒΠ³ΠΎΠΉ ΠΊΠΎΠ΄ private void wantChangeFragment() { if (onFragmentChangeListener!=null) onFragmentChangeListener.onFragmentChangeListener(); } } public class TwoFragment extends BaseFragment { //Ρ‚ΡƒΡ‚ происходит всякая инициализация ΠΈ Π΄Ρ€ΡƒΠ³ΠΎΠΉ ΠΊΠΎΠ΄ private void wantChangeFragment() { if (onFragmentChangeListener!=null) onFragmentChangeListener.onFragmentChangeListener(); } } 
    • Listen, do you have facebook or vk?) - Gend
    • Or write please for more details - Gend
    • until tomorrow I propose to think :) today I can not do anything. - andreich
    • Did not help) - Gend