android - How to close an activity on button click

2019-03-17 20:41发布

I want a button click to close an Activity. I am new to intents and and a little confused. This is ActivityOne which keeps a track of life cycle.
On a button press, it opens ActivityTwo and puts ActivityOne in background.
This works fine when I used this intent in my onClickListener:

Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class);
ActivityOne.this.startActivity(myIntent);

In ActivityTwo I need to use intents and finish() method to close the activity. I tried to Google it and I thought this might work:

Intent myIntent2 = new Intent(getApplicationContext(), ActivityTwo.class);
ActivityTwo.this.finish(myIntent2);

It didn't work the full code is below:

public class ActivityOne extends Activity {

    // ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        // ...

        Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo); 
        launchActivityTwoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO:
                // Launch Activity Two
                // Hint: use Context's startActivity() method

                // Create an intent stating which Activity you would like to start
                Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class);

                // Launch the Activity using the intent
                ActivityOne.this.startActivity(myIntent);               
            }
        });

        // ...
    }
}

Activity 2:

public class ActivityTwo extends Activity {    

    // ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);

        // ...

        Button closeButton = (Button) findViewById(R.id.bClose); 
        closeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO:
                // This function closes Activity Two
                // Hint: use Context's finish() method
                Intent myIntent2 = new Intent(getApplicationContext(), ActivityTwo.class);
                ActivityTwo.this.finish(myIntent2);
            }
        });
        // ...
    }
}

5条回答
啃猪蹄的小仙女
2楼-- · 2019-03-17 20:56

Alright the best way to finish the current Activity is by using finish() method. So inside the onClick() of your button in the ActivityTwo you can do this.

closeButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
            //closes ActivityTwo
        }
});

no need to make new intents for finishing current Activity. Also note that pressing back will also finish your Activity.

查看更多
一夜七次
3楼-- · 2019-03-17 21:01

@Arjun Krishnan you want to kill activityTwo?

try this

 ActivityTwo.this.finish();

It would help you

查看更多
ら.Afraid
4楼-- · 2019-03-17 21:01
Button exit= (Button) findViewById(R.id.yes);
exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.this.finish();
            }
        });
查看更多
放荡不羁爱自由
5楼-- · 2019-03-17 21:04

Replace these two lines

Intent myIntent2=new Intent(getApplicationContext(),ActivityTwo.class); ActivityTwo.this.finish(myIntent2);

with this ActivityTwo.this.finish();

查看更多
ら.Afraid
6楼-- · 2019-03-17 21:15

You want to close ActivityTwo when a button is clicked? Just use finish();

Full code for the button listener in ActivityTwo would be:

Button closeButton = (Button) findViewById(R.id.bClose); 
closeButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        // TODO:
        // This function closes Activity Two
        // Hint: use Context's finish() method
        finish();
    }
});
查看更多
登录 后发表回答