2011年7月11日月曜日

avoiding an exception with the back key

When the back key is pushed at the activity called by startActivityForResult(), the application will be stopped with an exception.  Then you need to override onBackPressed() and call setResult() in it.

Like this;


@Override
public void onBackPressed(){
Intent result = new Intent();
Spinner rounds = (Spinner)this.findViewById(R.id.CuNumRound);
result.putExtra("ROUNDS", Integer.parseInt((String)rounds.getSelectedItem()));
CheckBox bonus = (CheckBox)this.findViewById(R.id.CuBullBonus);
result.putExtra("BULLBONUS", bonus.isChecked());
setResult(RESULT_OK, result);
super.onBackPressed();
}

0 件のコメント:

コメントを投稿