0

I want to save value from second activity and add it to "int ha" in onActivityResult...

Second activity

backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Pataskhan.this, MainActivity.class);
            i.putExtra("hashiv", a);
            setResult(RESULT_OK, i);
            finish();
        }
    });

First activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data == null) {Toast.makeText(this,"Error", Toast.LENGTH_SHORT).show();}
    int pat = data.getIntExtra("hashiv", hash);
    int ha = 0;ha+=pat;
    hashivVerj.setText(Integer.toString(ha));
}

Initializing variable a

private int a = 0;

 public void equalsPat(boolean b){

    if(b == getIntent().getExtras().getBoolean("pat")){
        Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
        a+=1;

    }else{
        Toast.makeText(this, "InCorrect", Toast.LENGTH_SHORT).show();
        a-=1;

    }

}
Arsen Nersisyan
  • 458
  • 5
  • 18
  • 1
    Possible duplicate of [How to pass integer from one activity to another?](http://stackoverflow.com/questions/7074097/how-to-pass-integer-from-one-activity-to-another) – ghoulfolk Feb 26 '16 at 00:37
  • i think the answer to this question might help you solve the problem: http://stackoverflow.com/questions/7074097/how-to-pass-integer-from-one-activity-to-another in your onClick you set "a" as intValue. – ghoulfolk Feb 26 '16 at 00:38

2 Answers2

0

Update your code as below

Second activity // 5 is a constant used for identification

backBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Pataskhan.this, MainActivity.class);
            i.putExtra("hashiv", a);
            setResult(5, i);
            finish();
        }
    });
First activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==5)  
    {  
        if (resultCode == RESULT_OK) {
            if (data == null) {Toast.makeText(this,"Error", Toast.LENGTH_SHORT).show();}
            int pat = data.getCharExtra("hashiv", hash);
            int ha = 0;ha+=pat;
            hashivVerj.setText(Integer.toString(ha));
        }  
    }
}

    }
sanky jain
  • 873
  • 1
  • 6
  • 14
  • Sorry, but it isn't work... My code is working, but everytime when I open the second activity, "int ha" equls 0. – Arsen Nersisyan Feb 26 '16 at 03:26
  • please add your code where you assign value to variable a – sanky jain Feb 26 '16 at 04:18
  • private int a = 0; public void equalsPat(boolean b){ if(b == getIntent().getExtras().getBoolean("pat")){ Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show(); a+=1; }else{ Toast.makeText(this, "InCorrect", Toast.LENGTH_SHORT).show(); a-=1; } – Arsen Nersisyan Feb 26 '16 at 11:58
0

You should not create new Intent, instead use existing one by calling getIntent() method. After this should work.

backBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = getIntent();
        i.putExtra("hashiv", a);
        setResult(5, i);
        finish();
    }
});
someUser
  • 965
  • 12
  • 24