1

As I am not familiar with Fragments I am getting quite confused with the solutions presented on stackoverflow. I have tried many different techniques to achive this task: I have a class called MapFragment that extends Fragment. It works fine inside my viewpager. However, I want to reuse this class from a different activity. Heres a sample from my Fragment called MapFragment:

public class MapFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_mapview, container, false);
        mapView = (MapView) rootView.findViewById(R.id.fragment_mapView);
        mapView.onCreate(savedInstanceState);

        this.setHasOptionsMenu(true);

        activity.getActionBar().setDisplayHomeAsUpEnabled(true);

        initGPS();
        initViews();
        initListeners();

        Bundle extras = getArguments();

        String url = "";
        if(extras.containsKey("url"))
            url = extras.getString("url");

        new LoadMarkers().execute(url);

        return rootView;
    }
}

Let´s say I have another activity that I want to be used to reuse this mapfragment above:

public class MapActivity extends GeneralActivity {

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

        // I want to be able to display the MapFragment inside this activity
        // What Should I Do Here????
        enter code here
    }
}

Then I can from anywhere in my program call a intent on the MapActivity knowing it will display the MapFragment within it. Is this possible to be accomplished?

Gustavo Baiocchi Costa
  • 1,379
  • 3
  • 16
  • 34

2 Answers2

1

Create a layout for your activity class MapActivity. say activity_map.xml

Rough snippet

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/frag_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

In your activity class,

public class MapActivity extends GeneralActivity {

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

        // I want to be able to display the MapFragment inside this activity
        // What Should I Do Here????
        // enter code here
        setContentView(R.layout.activity_map);

        Fragment fragment = new MapFragment();
        FragmentManager manager = getSupportFragmentManager(); 

        manager.beginTransaction()
                .replace(R.id.frag_container, fragment)
                .commit();

    }
}

Also, in your fragment code, you are expecting a bundle. Make sure to check if this bundle is not null. Since with the above code, we are not passing any data (setArguments).

Hope this helps!

droidx
  • 2,172
  • 19
  • 26
  • Fragment fragment = new MapFragment(); -> It complains that the Object MapFragment is not of the type Fragment even though it extends fragment. BTW, my minSDK is api 16. – Gustavo Baiocchi Costa Apr 15 '15 at 13:22
  • check if its android.app fragments or the support fragment (android.support.v4.app) in your import packages. If you are using support fragments make sure you have extended the support Fragment class. see this : http://stackoverflow.com/questions/15109017/difference-between-android-app-fragment-and-android-support-v4-app-fragment – droidx Apr 15 '15 at 13:29
  • You are correct. The problem is that my view pager is using the support fragment. Then my MapFragment has to be using the support version as well. I changed it and the error cleared. However, another error appeared now over the FragmentManager -> getSupportFragmentManager() is undefined of the type MapActivity – Gustavo Baiocchi Costa Apr 15 '15 at 13:37
  • Your activity should extend FragmentActivity to be able to use methods like getSupportedFragmentManager() – droidx Apr 15 '15 at 14:21
  • I had to change my MapActivity to get rid of that error and extend the FragmentActivity to be able to get the support fragment manager. However, I get message now: java.lang.NoClassDefFoundError: com.google.android.maps.MapActivity – Gustavo Baiocchi Costa Apr 15 '15 at 14:45
  • I found the problem. GoogleMaps already has a class called MapActivity and therefore conflicting with my own class named similarly. Changed the name of the activity and everything started to work just fine! I am accepting your answer, many thanks! – Gustavo Baiocchi Costa Apr 15 '15 at 15:34
1

If your activity extends ActionBarActivity then:

setContentView(R.layout.some_layout);
if(savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction().add(containerViewId, new MapFragment(), "MapFragment").commit();
}

if not:

setContentView(R.layout.some_layout);
if(savedInstanceState == null) {
   getFragmentManager().beginTransaction().add(containerViewId, new MapFragment(), "MapFragment").commit();
}

where containerViewId is an id of your layout in activity (some_layout) where you want to place the fragment.

questioner
  • 2,283
  • 3
  • 26
  • 35