Dmytro
Dmytro

Reputation: 2319

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 can't figure out how to reopen my FAB after I press Back button and return to the view of the activity.

Upvotes: 0

Views: 472

Answers (2)

tarsom
tarsom

Reputation: 11

Instead of adding FAB on your Activity directly,add FAB on a fragment(FABFragment) and add another fragment(MessageFragment) for your message.

So whenever you want to show MessageFragment replace the FABFragment using following code:

MessageFragment fragment1 = MessageFragment.newInstance(item); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.addToBackStack(null); ft.replace(R.id.frame, fragment1);

To communicate between your fragments use interfaces(https://developer.android.com/training/basics/fragments/communicating.html)

Upvotes: 0

Isj
Isj

Reputation: 2070

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

Upvotes: 1

Related Questions