user1154644
user1154644

Reputation: 4607

Finishing an Activity in Android

When you press the back button while in an activity, by default, does the application not go back to the activity that called it? I am calling an activity in my application(call it Activity B), from Activity A, but when I hit the back button while in Activity B, I am taken back to the main page of the application.

So I guess in general, does pushing the back button on your phone take you to the calling activity?

Calling activity B from within an inner class of activity A:

    class HeadlineButtonListener implements OnClickListener {

    private Story story;

    public HeadlineButtonListener(Story story) {
        this.story = story;
    }

    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
        myIntent.putExtra(Constants.STORY_EXTRA, story);
        HeadlineBoard.this.startActivity(myIntent);
        finish();

    }
}

Upvotes: 0

Views: 131

Answers (1)

MByD
MByD

Reputation: 137442

You call finish() on first activity after firing the next activity, this will cause it to be removed from the activity stack, just remove the call to finish():

@Override
public void onClick(View v) {
    Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
    myIntent.putExtra(Constants.STORY_EXTRA, story);
    startActivity(myIntent);
}

Upvotes: 1

Related Questions