Linked Questions

Popular Questions

I made app that shows you random number after you select minimum and maximum number. But after rotating result disappears, so i implemented onSaveInstanceState and customized it for my need, and after that my app crashes all the time.

public class MainActivity extends AppCompatActivity {

EditText et_min,et_max;
Button button;
TextView tv_output;

Random r;
int min, max, output;
private static final String STATE_TV_OUTPUT = "tv_output";

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    String onSave = savedInstanceState.getString("onSave");

protected void onSaveInstanceState(Bundle SavedInstanceState) {
    SavedInstanceState.putString("onSave", "1");

protected void onCreate(Bundle savedInstanceState) {

    r = new Random();
    String onSave = tv_output.getText().toString();
    et_max = (EditText) findViewById(;
    et_min = (EditText) findViewById(;
    button = (Button) findViewById(;
    tv_output = (TextView) findViewById(;

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String tempMin, tempMax;
            tempMin = et_min.getText().toString();
            tempMax = et_max.getText().toString();
            if (!tempMin.equals("") && !tempMax.equals(""))//ovde stavljam ih u poziciju gde su slobodni za pisanje
                min = Integer.parseInt(tempMin);
                max = Integer.parseInt(tempMax);//ova dva sluze kako bi pretvorili stringove u intove za potrebe dole methoda
            if (max > min) {
                output = r.nextInt((max - min) + 1) + min;//nextInt sluzi kako bi dao random broj izmedju dva broja u ovom slucaju min i max
                tv_output.setText("" + output);



Related Questions