-2

I am developing an application where i need to have a toolbar at the top with bottom tabs and display Google Map in one of the tabs. I an using FragmentTabHost for the bottom Tabs and using Google Maps Api v2 for Google Maps with some custom image markers on it.I had done displaying the Google Maps on tab click but the main problem arises after this. I know for using toolbar as an Action bar i need to extend my Activity with ActionBarActivity but for using google maps i need to extend my Activity with FragmentActivity. I have tried a lot of things but to get a reference of Google Map in fragment i need to use this

SupportMapFragment supportMapFragment =
                (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
googleMap = supportMapFragment.getMap();

Now if i make my Activity extends ActionBarActivity them i will be able to use toolbar in my activity but getSupportFragmentManager() will return null as my activity was not extending FragmentActivity.Hope you guys understood my problem.Any help is highly appreciated.

Orton
  • 151
  • 1
  • 8
  • And what about extends `FragmentActivity` ? – M D May 05 '15 at 09:59
  • When i extends FragmentActivity and using Toolbar as Action bar then code setSupportActionBar(toolbar) gives a compile time error that setSupportActionBar is undefined as i have not extended my Activity with ActionBarActivity. – Orton May 05 '15 at 10:30

3 Answers3

1

If you check source of ActionBarActivity you will see that ActionBarActivity extends FragmentActivity. So you only need to extend ActionBarActivity. ActionBarActivity will work for you, and to make your app compatible with lower versions, you should import android-support-v7-appcompat in your project

Sajal
  • 1,452
  • 2
  • 14
  • 17
  • Thanks Sajal for your answer. I found the real issue as i was using SupportMapFragment inside a fragment that why in the code SupportMapFragment supportMapFragment = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); i was getting supportMapFragment as null. I found solution now i have to use getChildFragmentManager() instead of getSupportFragmentManager() – Orton May 05 '15 at 11:39
  • yeah if you want fragment manager inside a fragment , you should use getChildFragmentManager() instead of getSupportFragmentManager(). – Sajal May 05 '15 at 11:50
1

ActionBarActivity is already deprecated replace it with AppCompatActivity. This also subclasses FragmentActivity, so you can always perform all the operations as done in its parent.

Also, you can add a FrameLayout to your layout XML and then add a Map in it programatically, this will provide more control.

Adding in layout.xml:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/region_map_container" />

Adding Map programmatically

FragmentManager manager = getChildFragmentManager();
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
manager.beginTransaction().replace(R.id.region_map_container, mapFragment).commit();
Ankit Bansal
  • 1,801
  • 17
  • 34
0

ActionBarActivity already extends FragmentActivity so there is no problem here. getSupportFragmentManager() shouldn't be null. Is your setContentView OK? (Asking because of this).

Community
  • 1
  • 1
Damien
  • 141
  • 1
  • 6