antonpuz
antonpuz

Reputation: 3316

finish an activity gives error

I am a beginner at Android programming and I have a little problem when closing an activity: When I enter my app I navigate to another Activity called "camp", and I want to close the Activity when I come back to the menu screen, so I wrote this:

public void onPause(){
    CampActivity.this.finish();
}

but when I press the back button I get an error telling me that the application was closed.

What should I do so it will work?

Upvotes: 0

Views: 813

Answers (2)

A. AMAIDI
A. AMAIDI

Reputation: 6849

simply tapping the back button will close the Activity. or you can do it using a Button

 button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();

        }
    });

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

because CampActivity.this.finish(); will again call pause function (see activity life cycle ) so you should not call finish in onPause()

Upvotes: 2

Related Questions