alexhaifa
alexhaifa

Reputation: 5

Hide the Floating Action Button from my Fragment and keep it hide even I change the orientation of the phone

I could solve how to hide or show a Floating Action Button from a fragment when I call it. But I faced with another problem that I didn't know how to solve it, when I rotate my phone, the FAB appears again.

You can see my code below and how I did to hide my FAB, but how to keep it when my phone rotate from Portrait to Landscape?

package com.example.cursobaralhocigano.ui.deck;


import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;


import com.example.cursobaralhocigano.MainActivity;
import com.example.cursobaralhocigano.R;
import com.example.cursobaralhocigano.classes.cBaralhos;
import com.example.cursobaralhocigano.dao.uLibSql;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

/**
 * A simple {@link Fragment} subclass.
 */
public class DeckFragment extends Fragment implements View.OnClickListener {
    private uLibSql DB;
    private cBaralhos baralho = new cBaralhos();

    CheckBox ck01, ck02, ck03, ck04, ck05;
    ImageButton Img;

    public DeckFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        LinearLayout ln;
        View view = inflater.inflate(R.layout.fragment_deck, container, false);

        final FloatingActionButton fab = ((MainActivity) getActivity()).findViewById(R.id.fab);

        if (fab.isShown()) {
            fab.hide();
        }

return view;
}

Thanks a lot for help Regards Alex

Upvotes: 0

Views: 432

Answers (1)

Arunachalam k
Arunachalam k

Reputation: 744

You have to correctly save the instance state of Fragment you should do the following:

  1. In the fragment, save instance state by overriding onSaveInstanceState() and restore in onActivityCreated():

Below link may help you

https://stackoverflow.com/a/17135346/10239870

Upvotes: 1

Related Questions