How to make a floating action button pressed in a fragment?

so I have 3 fragments, in each fragment there is a button with a floating action, when clicked, it will go to another action.

what I did makes the floating action in mainActivity public, so I can use it for fragments. my code

public FloatingActionButton fab;


fab = (FloatingActionButton) findViewById (R. id. fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

code in fragment:

@Override
    public void setUserVisibleHint(boolean visible)
    {
        super.setUserVisibleHint(visible);
        if (visible && isResumed())
        {
            onResume();
        }
    }

    public void onResume()
    {
        super.onResume();
        if (!getUserVisibleHint())
        {
            return;
        }

        MainEventActivity mainActivity = (MainEventActivity)getActivity();
        mainActivity.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(this, MapActivity.class);
                startActivity(intent);
            }
        });
    }

I don’t know what to do next, I tried to put the floating action button in MainEvent.xml, it does not appear, but when I put it in the .xml fragment, it shows, but cannot click. Can you guys help me?

Btw I did this because I don’t know how to put googlemaps in a fragment, so I did a new activity for googlemaps.

+4

Source: https://habr.com/ru/post/1619749/


All Articles