1

I've tested everything and spent last 3 hour in fixing this bug. I don't why but my app deletes all data from textView. I made onSaveInstance and onRestore but it's the same problem.

I know i can fix this problem from onCreate but i'm not what exactly i need to do.

public class MainActivity extends AppCompatActivity {

    EditText et_min,et_max;
    Button button;
    TextView tv_output;

    Random r;
    int min, max, output;
    static final String OUT_PUT = "output";



    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        if (output!= 0){
            savedInstanceState.putInt(OUT_PUT, 0);
        }
        super.onSaveInstanceState(savedInstanceState);
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        output = savedInstanceState.getInt(OUT_PUT);
    }

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

        r = new Random();
        et_max = (EditText) findViewById(R.id.et_max);
        et_min = (EditText) findViewById(R.id.et_min);
        button = (Button) findViewById(R.id.button);
        tv_output = (TextView) findViewById(R.id.tv_output);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            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);
                }
            }

        });
    }
}
Pratik Butani
  • 51,868
  • 51
  • 228
  • 375

1 Answers1

1

Yes, you need to use onSaveInstanceState. This methods will save your variables which you have in your textViews or something else. In your case you need to declare your views like findViewById in onSaveInstanceState and onRestoreInstanceState. You can try something like this, this code is working:

public class MainActivity extends AppCompatActivity {

Button Btn;
TextView textView;
String cnt = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Btn = (Button) findViewById(R.id.button);
}

public void onBtnClick(View view) {
    textView = (TextView) findViewById(R.id.textView);
    textView.setText("Hehe");
}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    textView = (TextView) findViewById(R.id.textView);
    cnt = (String) textView.getText();
    outState.putString("count", cnt);
}

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    cnt = savedInstanceState.getString("count");
    textView = (TextView) findViewById(R.id.textView);
    textView.setText(cnt);
}
}
Valentin
  • 330
  • 1
  • 10