Using ActionBar Up Navigation Android - API23

1

I'm implementing an app that has a activity to load a map and another activity to list the places the user selected on the map.

As I'm working with API23 , the toolbar was successfully displayed on activity master.

I need to implement up navigation in% of% of maps , so that the user can return to the main if desired. The problem is that I can not use activity in this toolbar because it is inheriting a activity , which does not support FragmentActivity .

So I tried solving by manually creating a setSupportActionBar and finally, updating ActionBar . As shown below, nothing is displayed in ActionBar.

Main activity with ActionBar OK

MapsActivitywithActionBarwithnothing

MapsActivityClass

importandroid.app.ActionBar;importandroid.content.Intent;importandroid.os.Bundle;importandroid.support.v4.app.FragmentActivity;importandroid.util.Log;importandroid.view.MenuItem;importcom.google.android.gms.maps.CameraUpdateFactory;importcom.google.android.gms.maps.GoogleMap;importcom.google.android.gms.maps.OnMapReadyCallback;importcom.google.android.gms.maps.SupportMapFragment;importcom.google.android.gms.maps.model.LatLng;importcom.google.android.gms.maps.model.MarkerOptions;publicclassMapsActivityextendsAppCompatActivityimplementsOnMapReadyCallback{privateGoogleMapmMap;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_maps);//ObtaintheSupportMapFragmentandgetnotifiedwhenthemapisreadytobeused.SupportMapFragmentmapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);mapFragment.getMapAsync(this);Toolbartoolbar=(Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar);android.support.v7.app.ActionBaractionBar=getSupportActionBar();if(actionBar!=null){actionBar.setDisplayHomeAsUpEnabled(true);}Intentintent=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>
    
asked by anonymous 23.11.2015 / 23:56

1 answer

2

Change FragmentActivity by AppCompatActivity (which is a subclass of FragmentActivity with action bar support ).

So, in addition to being able to work with maps in your MapsActivity , you will have access to action bar using the getSupportActionBar() method (which should be used instead of getActionBar() ). p>

For this you will need to use the appcompat support library version 7 ( see here how to install ).

(EDITED) See if your styles.xml looks like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    ...
</style>

Also see if it works without <item> . Maybe it works without.

    
24.11.2015 / 00:50