Search code examples
androidandroid-intentonresumeonactivityresult

Can not getIntExtra() at onActivityResult


I got a problem with sending and receiving int types from one activity to another via Intent;

I'm sending it with onActivityResult() function which is placed at the receiving activity.

The code:

The Sending Activity:

Intent ba=new Intent();
        
MyPoints = fgv.getPoints();

int MP=(int)MyPoints;
Log.i("Problem","MyPoints MP = "+MP);

ba.putExtra("FocusScore",MP);
Log.i("Problem","MyPoints = "+MP);

setResult(RESULT_OK,ba);
finish();

The Receiving Activity:

//At the onClick in order to move the the other class

Intent goFocus =new Intent(Games.this,FocusGame.class);

startActivityForResult(goFocus,1);

//At the onActivityResult function

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        
        if(resultCode==RESULT_OK)
        {   
            
          switch (requestCode) {
            
             case 1: {
             //Coming back from Focus-Game
             //Problem:!!
                
              int sss= getIntent().getIntExtra("FocusScore", -1); 
              Log.i("Problem","sss = "+sss);
            
                 }
             break;
            
             default :
             break;
            
            }

}

The result of the code is given a log where sss=-1. Which means that

getIntent().getIntExtra();

is always null.

And the Log of the MP is working fine.

-

Hope you could help me over here.

- Thanks in advance, Yaniv.


Solution

  • Use the value of data not getIntent()

    if (data != null) {
        //int sss= getIntent().getIntExtra("FocusScore", -1); 
        Bundle extras = data.getExtras();
        int sss =  extras.getInt("FocusScore");
    }
    

    or just

    if (data != null) {
        //int sss= getIntent().getIntExtra("FocusScore", -1);     
        int sss= data.getIntExtra("FocusScore", -1); 
    }
    

    More info: Cannot get Data from the intent - Android