Dmitry Smolyaninov Dmitry Smolyaninov - 1 month ago 16
Android Question

Hide/Show floating action button during FragmentTransection

I have this code for open some Message Fragment.

private void openMessage(int position) {
MessageRecord item = data.get(position);
item.setIsRead(true);
item.save();
List<MessageRecord> tmp = new ArrayList<>(data);
updateScreen(tmp);

MessagesActivity2 parentActivity = ((MessagesActivity2) context);
parentActivity.fab.hide();
FragmentTransaction ft = parentActivity.getSupportFragmentManager().beginTransaction();

ft.addToBackStack(null);
MessageFragment fragment1 = MessageFragment.newInstance(item);
ft.add(R.id.frame, fragment1);

ft.commit();

}


And this line hides my fab.
parentActivity.fab.hide();

But I cant figure out how to reopen my FAB after i press Back button and return to the view of the activity.
Thanks in advance for help, guys!

Isj Isj
Answer

Override onBackPressed() in the activity and inside check if the fragment is null or not as per your requirement, then hide your FAB.