How to access methods of a Fragment from an Activity?

0

I have an Activity that contains a Fragment (and this Fragment is Google Map). I want for example, having a button in the Activity change the map type to Satellite. How could I do this through this Button?

Activity:

public class MainActivity extends AppCompatActivity {

    private FragmentManager fragMan;
    private FragmentTransaction ftrans;
    private FloatingActionButton fab;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.btm_route:
                    // TODO



                    fab.hide();

                    getSupportActionBar().setTitle(R.string.rota_fragment_titulo_actionBar);

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment, new RotaFragment())
                            .commit();


                    return true;
                case R.id.btm_localizacao:
                    // TODO

                    getSupportActionBar().setTitle(R.string.localizacao_fragment_titulo_actionBar);

                    fab.show();

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment, new MapFragment())
                            .commit();

                    return true;
                case R.id.btm_fuel:
                    // TODO

                    getSupportActionBar().setTitle(R.string.fuel_fragment_titulo_actionBar);

                    fab.hide();

                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment, new FuelFragment())
                            .commit();

                    return true;
            }
            return false;
        }
    };



    public void setupComps() {

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setSelectedItemId(R.id.btm_localizacao);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(fabListener);

    }


    public void configurarFragments() {

        fragMan = getSupportFragmentManager();
        ftrans = fragMan.beginTransaction();

        ftrans.add(R.id.fragment, new MapFragment(), "MapaFragmento");
        ftrans.commitAllowingStateLoss();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getSupportActionBar().setTitle(R.string.localizacao_fragment_titulo_actionBar);

        setContentView(R.layout.activity_main);

        configurarFragments();


        if (savedInstanceState == null) {


            configurarFragments();

           /* getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment, new MapFragment())
                    .commit(); */


        }

        setupComps();



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main_actvity_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {




        switch (item.getItemId()) {



            case  R.id.terreno_maptype:
                //TODO





                break;

            case R.id.satelite_maptype:

                //TODO

                break;


            case R.id.hibrido_maptype:

                //TODO

                break;



            case R.id.sobre_menu:

                Intent sobre = new Intent(this, SobreActivity.class);
                startActivity(sobre);

                break;

        }

        return super.onOptionsItemSelected(item);
    }

    View.OnClickListener fabListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {



        }
    };

}

And I have this Fragment that is the one on the map:

public class MapFragment extends Fragment implements OnMapReadyCallback  {

    private GoogleMap map;
    private View view = null;

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


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

        setupComps();

        return view;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    public void setupComps() {

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);


    }



    public void configurarMapa() {

        map.getUiSettings().setZoomControlsEnabled(true);
        map.getUiSettings().setRotateGesturesEnabled(true);

    }


    public void alterarTipoParaSatelite() {

        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    }

    public void alterarTipoParaHibrido() {

        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    }

    public void alterarTipoParaTerreno() {

        map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

    }

    public void apontarOMarker() {

        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        // Add a marker in Maputo and move the camera
        LatLng yourCar = new LatLng(-25.962803, 32.583162);
        map.addMarker(new MarkerOptions().position(yourCar).title("Seu veículo está aqui"));

        CameraPosition position = new CameraPosition.Builder()
                .target(yourCar)
                .zoom(15)
                .build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(position));

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {

        map = googleMap;

        apontarOMarker();

        configurarMapa();

    }
}
    
asked by anonymous 26.04.2018 / 17:01

0 answers