Antonio Gschossmann
Antonio Gschossmann

Reputation: 622

Navigation Bar click event - Android Application

I'm trying to handle a onClick event on the Navigation Bar in an Android Application.

Exactlyer, when the user clicks on the return button the Application must go back to the Main Activity ( in my case "Start" ) and not to the previous Activity.

In my application I have more Activities, which are all callable from every Activity, so when, for example, I'm in "Start", I click a Button and I come to "Karte", then from "Karte" I want to go to "Einstellungen", then from "Einstellungen" I want return to the Main Activity "Start", but I can't because when I click on the return button on the Nav. Bar I come only back to the previous Activity ("Karte").

If anyone knows how to handle this, please answer.

Upvotes: 1

Views: 4075

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

1. After starting Einstellungen from Karte, just finish Karte activity to remove it from stack:

//Karte.java 

Intent intentEinstellungen = new Intent(karte.this, Einstellungen.class);
startActivity(intentEinstellungen);

// Finish Karte
finish();

2. When navigation back/home icon pressed from Einstellungen, just call super.onBackPressed() from method onOptionsItemSelected() to finish the Einstellungen activity.

//Einstellungen.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case android.R.id.home:
            super.onBackPressed();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

It will show Start because the Karte already pop out from stack.

Upvotes: 2

Jonathan Aste
Jonathan Aste

Reputation: 1774

Try this:

On your "Einstellungen" activity, override the onBackPressed method to handle navigation:

@Override
public void onBackPressed() {
    Intent i = new Intent(this, Start.class);
    startActivity(i);
    finish();
}

When you navigate from "Karte" to "Einstellungen" and from "Start" to "Karte" you should finish() your activity too to avoid navigation issues.

Upvotes: 2

Related Questions