Skip to content

Instantly share code, notes, and snippets.

@antonioxtasis
Last active September 11, 2018 20:57
Show Gist options
  • Save antonioxtasis/eab8c3617d005dc4c2b0d2b7ce38e2e1 to your computer and use it in GitHub Desktop.
Save antonioxtasis/eab8c3617d005dc4c2b0d2b7ce38e2e1 to your computer and use it in GitHub Desktop.
Adnroid - Activity and Fragment communication using inteface
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/colorBlack"
app:srcCompat="@drawable/plus" />
</android.support.design.widget.CoordinatorLayout>
public class MyActivity extends implements MyFragment.MyFragmentListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_with_fragment);
MyFragment myFragment = new MyFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
fragmentTransaction.commit();
FloatingActionButton floatOptionButton = findViewById(R.id.fab_add);
floatOptionButton.setOnClickListener(v -> myFragment.someMethod());
}
...
@Override
public void onFragmentButtonClicked() {
// stuff - when fragment respond
}
}
public class MyFragment extends Fragment {
public MyFragment(){ }
public static Fragment newInstance() {
return new MyFragment();
}
// Interface
private MyFragmentListener mCallback;
public interface MyFragmentListener {
void onFragmentButtonClicked();
}
@Override
public void onAttach(Context activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (MyFragmentListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
@Override
public void onDetach() {
super.onDetach();
mCallback = null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
someButton.setOnClickListener(view -> {
mCallback.onFragmentButtonClicked();
});
}
public void someMethod() {
// stuff - it can be called from activity
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment