Search code examples
androidonactivityresult

Android how receive more data with onActivityResult


i start my Dialog Activity with startActivityForResult(myIntent, 1); for take back some data. The protected void onActivityResult(int requestCode, int resultCode, Intent data) can take back only one type of data?

I need to take back a string(from an EditText) and a resource color(Background of a RadioButton) Can i put all two into returnIntent.putExtra("result",result);?? Or can i send only one thing?

This is the code of the Dialog Activity

public class ActivityAddMateria extends Activity {

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

    final Button exit_button = (Button) findViewById(R.id.exit_dialog_materia);
    exit_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //No input
            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            //Exit from Dialog
            finish();
        }
    });

    final Button accept_button = (Button) findViewById(R.id.add_materia);
    accept_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //Find EditText for take data
            EditText nome_materia = (EditText)findViewById(R.id.nome_materia);
            //Put result into variable result that is send back
            String result = nome_materia.getText().toString();; 

            Intent returnIntent = new Intent();
            returnIntent.putExtra("result",result);
            setResult(RESULT_OK,returnIntent);
            // Exit to Dialog
            finish();
        }
    });

 }
}

And this is the code of the Activity that take the input:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){

            String result = data.getStringExtra("result");

            Materia materia;
            materia = new Materia();
            materia.setMateria(result);
            materia.setColor(getResources().getColor(R.color.verde));
            DB.getMaterie().add(materia);

            LinearLayout mLayout = (LinearLayout) findViewById(R.id.contenitorematerie);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
            params.setMargins(30, 0, 0, 0);
            params.width = getResources().getDimensionPixelSize(R.dimen.text_view_width);

            for(int i=0; i< DB.getMaterie().size(); i++)
            {
                textView = new TextView(this);
                textView.setText(DB.getMaterie().get(i).getMateria());
                textView.setBackgroundColor(DB.getMaterie().get(i).getColor());
                textView.setGravity(Gravity.CENTER); //centro la scritta nella textview
                textView.setLayoutParams(params);   //assegno i parametri impostati sopra
                mLayout.addView(textView);
            }

        } 

        if (resultCode == RESULT_CANCELED) {
            //Nessuna materia inserita
        }
    }
}//onActivityResult

I need to set the color dynamically from results here materia.setMateria(result); materia.setColor(getResources().getColor(R.color.verde));


Solution

  • Can i put all two into returnIntent.putExtra("result",result);?

    You are welcome to put in two extras, using different keys (e.g., "result" and "this is another extra").