1
I’m implementing an app that has a activity
to load a map and another activity
to list the places the user has selected on the map.
How I’m working with API23
, Toolbar was successfully aired on activity
leading.
I need to implement the up navigation
in activity
of maps, so that the user can go back to main if they wish. The problem is that I cannot use toolbar
in that activity
Because she’s inheriting a FragmentActivity
, who can’t stand the setSupportActionBar
.
So I tried to solve by manually creating a ActionBar
and finally, updating the Manifest
. As shown below, nothing is displayed in Actionbar.
Main activity with Actionbar OK
Mapsactivity with Actionbar with nothing
Mapsactivity class
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Intent intent = getIntent();
Log.i("InfoLocalizacao", Integer.toString(intent.getIntExtra("InfoLocalizacao", -1)));
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="luizugliano.com.br.lugaresfavoritos">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"
android:parentActivityName=".MainActivity" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
</manifest>
You need to set on the map screen the back button, right? And the
getActionBar().setDisplayHomeAsUpEnabled(true);
simply does not work in your Fragmentactivity, correct?– Luiz
@Luiz exactly that.
– Luiz Henrique Ugliano
Change
FragmentActivity
forAppCompatActivity
and usegetSupportActionBar()
instead ofgetActionBar()
.– Piovezan
@The problem is I need to give the
extend FragmentActivity
for the map holders (as published in my question). As I can extend the two?– Luiz Henrique Ugliano
AppCompatActivity
already extendsFragmentActivity
, and this also includes support for maps.– Piovezan