finish()不会在第一时间关闭当前活动

时间:2017-08-16 06:38:50

标签: java android

我正在尝试开发一个带有两个活动的应用程序,它们会相互发送数据。问题是我想finish();第二次活动。当我点击我在XML_ SecondActivity中定义的goBackButton_时,当我再次点击该按钮时,它会回到我想要的MainActivity。为什么?以及如何解决它?为什么它第一次不起作用?

public class MainActivity extends AppCompatActivity {
Button button;
int REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,SecondActivity.class);
            intent.putExtra("FirstActivity", "Hello from first activity");
            startActivity(intent);

            startActivityForResult(intent, REQUEST_CODE);
            Log.d("TAG", "onClickListener: done!");
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    Log.d("TAG", "onActivityResult: Entered");
    if(requestCode == REQUEST_CODE) {
        Log.d("TAG", "onActivityResult: requestCode is OK");
        if (resultCode == RESULT_OK) {
            Log.d("TAG", "onActivityResult: result is returned OK");
            String result = data.getStringExtra("SecondActivity");
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();
            Log.d("TAG", "onActivityResult: done!");
        }
    }






}

}

public class SecondActivity extends AppCompatActivity {
TextView textView;
Button goBackButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    goBackButton = (Button) findViewById(R.id.goBackButton);
    goBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("TAG", "onClick: Entered");
            Intent returnIntent = getIntent();
            returnIntent.putExtra("SecondActivity", "from second Activity");
            setResult(RESULT_OK, returnIntent);
            Log.d("TAG", "onClick: Message sent");
            finish();
            Log.d("TAG", "onClick: Activity finished");
            //Intent myIntent = new Intent(SecondActivity.this,MainActivity.class);
            //startActivity(myIntent);

        }
    });
    textView = (TextView)findViewById(R.id.textView);
    Bundle extras = getIntent().getExtras();
    if(extras != null){
        String string = extras.getString("FirstActivity");
        textView.setText(string);
    }






}

}

解决

3 个答案:

答案 0 :(得分:3)

问题在于您的代码 -

startActivity(intent);

startActivityForResult(intent, REQUEST_CODE);

当您想startActivityForResult时,您不必执行startActivity,因为这将首先打开您的第二项活动,而不会查找结果。

删除startActivity(intent);

答案 1 :(得分:3)

您已添加startActivity(intent)以及startActivityforResult(intent, REQUEST_CODE),它会创建仅使用startActivityforResult(intent, REQUEST_CODE)的活动的双重实例;

答案 2 :(得分:2)

尝试删除 startActivity(意图);

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
        intent.putExtra("FirstActivity", "Hello from first activity");
        startActivityForResult(intent, REQUEST_CODE);
        Log.d("TAG", "onClickListener: done!");
    }
});