Working with Navigation Bar (smartphone native) and Action Bar BACK buttons (added via Java)

Asked

Viewed 10,546 times

2

I am Asking this question/answer to help those who may have/have/have some doubt regarding the buttons BACK that are extremely useful in our applications. I will use 1 and 2 to differentiate Action Bar (1) and Navigation Bar (2) Action e Navigation - BAR

Thank you very much to the users here of Stack who helped me to find this solution that were: Jean Felipe D. Silva and Regmoraes

3 answers

8


1. Back - Action Bar

  • Within the method onCreate(){ add the following lines:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Mostrar o botão
    getSupportActionBar().setHomeButtonEnabled(true);      //Ativar o botão
    getSupportActionBar().setTitle("Seu titulo aqui");     //Titulo para ser exibido na sua Action Bar em frente à seta
    
  • Out of method oncreate(){} add:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) { //Botão adicional na ToolBar
    switch (item.getItemId()) {
        case android.R.id.home:  //ID do seu botão (gerado automaticamente pelo android, usando como está, deve funcionar
            startActivity(new Intent(this, SuaActivity.class));  //O efeito ao ser pressionado do botão (no caso abre a activity)
            finishAffinity();  //Método para matar a activity e não deixa-lá indexada na pilhagem
            break;
        default:break;
    }
    return true;
    }
    

All set, you button Action Bar is working!

2. Back - Navigation Bar

  • Out of method onCreate(){}

    @Override
    public void onBackPressed(){ //Botão BACK padrão do android
    startActivity(new Intent(this, MainActivity.class)); //O efeito ao ser pressionado do botão (no caso abre a activity)
    finishAffinity(); //Método para matar a activity e não deixa-lá indexada na pilhagem
    return;
    }
    

Everything should be working perfectly now

0

You can resolve this in the manifest by adding this attribute to the current Activity

android:parentActivityName=".activity.PrincipalActivity"

0

Complementing the response of Erasmus and adding the attribute parentActivityName, also add the attribute launchMode in the block that is intended for the main screen. I will put a code below better describing my answer.

Where the manisfest of its application:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exemplo.ExemploDeCodigo">


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">

        <activity
            android:name=".SegundaActivity"
            android:parentActivityName=".MainActivity" >
        </activity>

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Each time a new Activity is added to your project, you add the parentActivityName pointing to the Activity that you wish to return.

Another interesting point is that if you don’t want to lose any data from a screen and you want it to stay the same by moving to the next screen and then returning, just use the android:launchMode="singleTop", this will cause the data to be kept.

Browser other questions tagged

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