package com.example.miguel.saludoforresulticon;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import saludoforresulticon.R;

public class Saludo extends AppCompatActivity {

    Button btnSalir;
    TextView etName;

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

        btnSalir=(Button)findViewById(R.id.btnSalir);
        etName= (TextView)findViewById(R.id.etName);

        Bundle extras = getIntent().getExtras();
        if (extras != null){
            String saludostring=extras.getString("txtSaludo");
            etName.setText(saludostring);
        }

        btnSalir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ComponentName callingActivity = Saludo.this.getCallingActivity();
                if (callingActivity != null) {
                    SetActivityResult();
                }
                else {
                    Toast.makeText(Saludo.this,"Sin retorno de información",Toast.LENGTH_LONG).show();
                }
                finish();
            }//onClick
        }); //setOnClickListener

    }//onCreate

    private void SetActivityResult(){
        Toast.makeText(Saludo.this, "Retornando información", Toast.LENGTH_LONG).show();
        Intent returnIntent = new Intent();
        returnIntent.putExtra("txtResult",etName.getText().toString());
        setResult(Activity.RESULT_OK, returnIntent);
    } //SetActivityResult


    @Override
    public void onBackPressed(){
        ComponentName callingActivity = Saludo.this.getCallingActivity();
        if (callingActivity != null) {
            SetActivityResult();
        }
        else {
            Toast.makeText(Saludo.this,"Sin retorno de información",Toast.LENGTH_LONG).show();
        }
        super.onBackPressed();
    }

}
