user3110137
user3110137

Reputation: 147

android up navigation bar always creates new activities

I have this architecture:

Activity 1 -> Activity 2

Activity 2 has a navigation bar with the up navigation button. When I press it, Activity 1 is created from scratch although the back button just goes back to Activity 1.

What should I do in order to make the navigation up not create Activity 1 from scratch?

Upvotes: 3

Views: 235

Answers (2)

coder
coder

Reputation: 13250

Actually I think you are creating a new Intent and navigating it instead of doing that you need to call finish() then check.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

Upvotes: 2

gunar
gunar

Reputation: 14710

You should have this in onOptionsItemSelected:

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

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

Upvotes: 3

Related Questions