Rattletrap
Rattletrap

Reputation: 113

Changing my Google Map from type FragmentActivity to a simple Fragment for a BottomNavigationView?

I wanted to implement a BottomNavigationView (toolbar at the bottom) in my app. My GoogleMap fragment is of type FragmentActivity, not fragment. Would this be a problem? Is there an easy way to convert it to a fragment? I've checked a lot of tutorials and none of them fit the situation I'm in or address it at all. Thanks in advance.

Upvotes: 0

Views: 32

Answers (1)

Ervin
Ervin

Reputation: 336

Hope this answer can help you !

Example Activity

public class ExampleActivity extends AppCopmactActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example_activity);

        initFragment(savedInstanceState);

    }

    private void initFragment(Bundle savedInstanceState) {

        if (findViewById(R.id.mainFragmentContainer) != null) {

            if (savedInstanceState != null) {
                return;
            }
            ExampleFragment exampleFragment = new ExampleFragment ();
            getSupportFragmentManager().beginTransaction().add(R.id.mainFragmentContainer, exampleFragment ).commit();
        }
    }

Example Fragment

   public class ExampleFragment extends Fragment{

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_example, container, false);
            return view;
        }


        @Override
        public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

 }

} 

example_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.Base">

    </android.support.v7.widget.Toolbar>

    <FrameLayout
        android:id="@+id/mainFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tool_bar" />

</LinearLayout>

Upvotes: 1

Related Questions