package jp.gihyo.mitoma.android.s010901basictrans;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import jp.gihyo.mitoma.android.s010901basictrans.extra.Activity_B;

public class Activity_A extends AppCompatActivity {
    EditText etValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_a);
        setTitle("A画面");
        etValue = findViewById(R.id.etValue);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new ButtonClick());
    }
    private class ButtonClick implements View.OnClickListener{
        public void onClick(View view){
            // Activity_Bへ遷移
            String message = etValue.getText().toString();
            Intent intent = new Intent(getApplicationContext(), Activity_B.class);
            intent.putExtra("message",message);
            // 遷移(非推奨のため取り消し線アリ)
            startActivityForResult(intent,100);
        }
    }
    @Override
    // 戻り値の取得
    protected void onActivityResult(int reqCode,int resultCode,Intent intent){
        super.onActivityResult(reqCode, resultCode, intent);
        if(resultCode == RESULT_OK && intent != null){
            // 戻り値の取り出しと表示
            String message = intent.getStringExtra("message");
            etValue.setText(message);
        }else{
            Toast.makeText(this, "遷移先異常発生", Toast.LENGTH_LONG).show();
        }
     }
}