From the documentation
Since each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in several actions, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
, , . - . , . Fragment s - , .
... . , , , , .
# 1:
, A, , B. , :
:
public interface OnButtonClickedListener {
public void onButtonClicked();
}
:
public class SampleActivity extends Activity implements OnButtonClickedListener {
public void onButtonClicked() {
}
}
A:
public class FragmentA extends Fragment {
OnButtonClickedListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnButtonClickedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnButtonClickedListener ");
}
}
public void clickButton() {
mListener.onButtonClicked();
}
}
# 2:
: " - - ? , A, B?"
, , - , . , . , , . , , , . , , B . , B, , , B .
, ... : P. ... , , - .