本文介绍了android-如何在单击按钮时关闭活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望单击按钮以关闭活动.我是新来的,有些困惑.这是ActivityOne,用于跟踪生命周期.
按下按钮时,它将打开ActivityTwo并将ActivityOne置于后台.
当我在onClickListener

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);

ActivityTwo中,我需要使用intent和finish()方法来关闭活动.我尝试使用Google进行搜索,但我认为这可能有效:

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);
            }
        });

        // ...
    }
}


活动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);
            }
        });
        // ...
    }
}

推荐答案

您要在单击按钮时关闭ActivityTwo吗?只需使用finish();

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

ActivityTwo中按钮侦听器的完整代码为:

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();
    }
});

这篇关于android-如何在单击按钮时关闭活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:41