Feras
Feras

Reputation: 75

is there a way to display an activity within a fragment

I am using a navigation drawer in my Map application, I managed to display the map on the main page. The problem am having is that I cannot connect the map fragment (in the layout "xml") to the MapsActivity (java file).

I tried to use MapsActivity extends Fragment instead of MapsActivity extends FragmentActivity but of course this does not work.

the main xml layout:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


 <!-- *****The problem I am having is in the following fragment could not link it to the MapsActivity-->
 <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/map"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>


</FrameLayout>


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.example.famfelimban.mydiet.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

However, I have a separate xml layout for the map which is working fine activity_maps.xml which is working fine with the MapsActivity

<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment"/>

MapsActivity

    public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

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

    @Override
    protected void onResume() {

    }

    private void setUpMapIfNeeded() {

    }

    private void setUpMap() {

    }
}

Upvotes: 0

Views: 1504

Answers (2)

h0x0
h0x0

Reputation: 495

What you want to do here is port your Activity code into a Fragment. It's not hard, a fragment has an almost identical lifecycle. The only difference is instead of grabbing and modifying the Views in onCreate you need to do it in onCreateView and you need to implement a static newInstance() method.

Leave the FrameLayout in the xml empty. It's only there for the FragmentManager to inflate the fragment into. Two changes to code

in the Activity

@Override
protected void onStart() {
    super.onStart();

    Fragment mapFragment = MapFragment.newInstance(/*args*/);
    getFragmentManager()  //you may need getSupportFragmentMananger()
            .beginTransaction()
            .replace(R.id.container, mapFragment)
            .commit()
    //this inflates the Fragment into the FrameLayout
}

and make a Fragment that functions like your Activity version

public class MapFragment extends Fragment {
    ...
    public MapFragment() {/*should be empty*/}
    ...
    public static MapFragment newInstance(/*args*/) {
        Fragment fragment = new MapFragment();

        Bundle args = new Bundle();
        //put whatever information you need for your activity to run here
        fragment.setArguments(args);
        return fragment;
    }
    ...
    @Overide
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        //this basically takes the place of onCreate in the Activity version
        View root = inflater.inflate(R.layout.fragment_map, parent);

        View yourView = root.findViewById(R.id.your_view);
        ...
        return root;
    }
    ...
}

Good luck.

Upvotes: 0

ajacian81
ajacian81

Reputation: 7569

No, Activitys are holders for Fragments, and Fragments can't hold Activities.

This question might be of interest: How to show different Activities in Fragment implementation?

Upvotes: 1

Related Questions