Azkan
Azkan

Reputation: 1

how to manipulate results from onBackPressed?

I'm trying to develop a simple scoreKeeper app. The problem is I don't know how can I manipulate the results from onBackPressed. I mean to add or substract. I tried to convert into an integer and to display in a textView but that didn't work.

PS: to receive data from intent and simple display on the screen I succeeded.

Here is my code: that's an example from my second intent which works fine

@Override
    public void onBackPressed() {
        Intent i2 = new Intent();
        i2.putExtra("message2", mEditText.getText().toString());
        setResult(RESULT_OK, i2);

and MainActivity

if (requestCode == 2 && resultCode == RESULT_OK) {
            mSecondscore.setText(data.getStringExtra("message2"));

but I want something else as I said here is my code:

public void onBackPressed() {

                Intent i = new Intent();
                i.putExtra("message", mEditText.getText().toString());
                setResult(RESULT_OK, i);
                Toast.makeText(this, "You added " + mEditText.getText().toString() + " points", Toast.LENGTH_LONG).show();
            finish();

and MainActivity

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

  /*          String a = data.getStringExtra("message");
           int i = Integer.parseInt(a);
           String b = data.getStringExtra("message5");
             int z = Integer.parseInt(b);
             int fResult = i - z;
            String firstResult = Integer.toString(fResult);
            mFirstScore.setText......
  */

            textV.setText(data.getStringExtra("message"));
            int a = Integer.parseInt(textV.getText().toString());
            int b = a + 10;
            mFirstScore.setText(Integer.toString(b));

Upvotes: 0

Views: 49

Answers (1)

Vir Rajpurohit
Vir Rajpurohit

Reputation: 1937

From the code you have posted; Error is in below line

mFirstScore.setText(Integer.toString(b));

Use below code

mFirstScore.setText(String.valueOf(b));

For the error you have commented You might be missing below code

    TextView textV= findViewById(R.id.your_id);

your_id is id you have assigned in XML.

Upvotes: 1

Related Questions