finish() does not close current activity at first time

I'm trying to develop an app with two activities which they send data to each other. the problem is that I want tofinish(); second activity. when I click on goBackButton_ which I defined in XML_ SecondActivity comes up, when I click on the button again, it comes back to MainActivity as I wanted. why? and How to solve it?why it doesn't work at first time?

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






}

}

solved

Upvotes: 0

Views: 1509

Answers (3)

Shashwat Gupta
Shashwat Gupta

Reputation: 872

you have added startActivity(intent) as well as startActivityforResult(intent, REQUEST_CODE) which creates double instance of an activity use only startActivityforResult(intent, REQUEST_CODE);

Upvotes: 3

Kapil G
Kapil G

Reputation: 4141

The problem is in your code -

startActivity(intent);

startActivityForResult(intent, REQUEST_CODE);

When you want to do startActivityForResult you don't have to do startActivity as this will first open your second activity without looking for result.

Remove startActivity(intent);

Upvotes: 3

Nguyễn Trung Hiếu
Nguyễn Trung Hiếu

Reputation: 2032

Try remove startActivity(intent);

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

Upvotes: 2

Related Questions