second_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/f2_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/f2_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/f2_tv" />
<Button
android:id="@+id/f2_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button"
android:text="@string/f2_bttn" />
Run Code Online (Sandbox Code Playgroud)
SecondFragment.java
public class SecondFragment extends Fragment {
FragmentInterface iface;
public interface FragmentInterface {
public void buttonPressed();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.second_fragment, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an …Run Code Online (Sandbox Code Playgroud)