Back Button in ActionBar at MainActivity

1.3k views Asked by At

I have implemented a Back-Button for all Activities except the MainActivity. My Problem is that i have a Back-Button in that MainActivity too.

Hopefully I've imported the right class:

    import android.support.v4.app.NavUtils;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        [...]
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        case R.id.ueber:
            startActivity(new Intent(this, menu_main_Activity.class));
            return true;
    }
    return super.onOptionsItemSelected(item);
}

AndroidManifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/levox"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ListViewActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ListTasksActivity"
        android:label="@string/projekte"
        android:parentActivityName=".ListViewActivity">
        </activity>
    <activity android:name=".ListSingleTaskActivity"
        android:label="@string/tasks"
        android:parentActivityName=".ListTasksActivity">
    </activity>
    <activity android:name=".menu_main_Activity"/>
</application>

Why i get a Back-Button at MainActivity too?

2

There are 2 answers

1
theMfromA On BEST ANSWER

The first question is: What do you mean by "Back-Button"?

  • Do you mean the button which is in each Android-Device on the bottom-left? You can't disable that one. But it is called "Back-Button" in the Android-universe actually. Sometimes it is even part of the hardware!
  • Do you mean the button which is shown by a little arrow directing to the left, just next to the App-Icon on the top left of your application? That button is called "Up-Button" in the Android-universe. This one is shown in your MainActivity because of this line in your code:

    getActionBar().setDisplayHomeAsUpEnabled(true);
    

    in the method

    protected void onCreate(Bundle savedInstanceState)
    

    So you should take away this line. To disable not just the appearance of the button but also the functionality of it, you have to take a look at this part of the code:

     switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    case R.id.ueber:
        startActivity(new Intent(this, menu_main_Activity.class));
        return true;
    }
    

    Here, you have to remove the part

    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    

EDIT: But if you actually want to have the "Up-Button" visible inside an Activity, you

  • have to set the code

    getActionBar().setDisplayHomeAsUpEnabled(true);
    

    in the onCreate()-method of that Activity!

  • provide a parent-Activity in your Manifest.xml (what you're doing already), for example:

    android:parentActivityName=".ListViewActivity"
    
  • and put the part

    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    

    into the Activity which shall have got the "Up-Button".

If you mean anything else, please specify your question and provide more code :)

Some useful links:

1
MUHOOZI DENIS On

Am using java and my minSdk = 19 and targetSdk =31. I added the following code inside the onCreate method

  getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Then i overide onSupportNavigateUp(). outside onCreate. as follows.

@Override
public boolean onSupportNavigateUp() {
    onBackPressed(); // goto previous activity when actionbar backbutton clicked
    return super.onSupportNavigateUp();
}