Use of Actionbar Up Navigation Android - API23

Asked

Viewed 365 times

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

inserir a descrição da imagem aqui

Mapsactivity with Actionbar with nothing

inserir a descrição da imagem aqui

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 exactly that.

  • Change FragmentActivity for AppCompatActivity and use getSupportActionBar() instead of getActionBar().

  • @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?

  • AppCompatActivity already extends FragmentActivity, and this also includes support for maps.

1 answer

2


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

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

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


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

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

See also if it works without the <item>. Maybe it works without.

  • I edited the class MapsActivity of my question, with the new changes suggested. The problem is that it is giving FATAL EXCEPTION when accessing the class - java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

  • I edited the answer, see if it solves the problem. Any questions, post your Styles.xml.

  • Bingo, I had to change the Styles.xml. Thank you very much. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.