简体   繁体   中英

Bundle null onActivityResult

I have tried a couple of different solutions already given but none works as everything seems to work fine in another activity that I am returning a result from, here is the code.

My Main activity where City activity is called:

//this method gets called on a button click and it works as other activity shows up

public void getCity(View v){
    Intent intent = new Intent(getApplicationContext(), City.class);
    startActivityForResult(intent,1); 
}
//receiving the data the first data is ok  but the second one is null although doing the same thing in both files
 protected void onActivityResult(int requestCode, int resultCode, Intent data){

    if(resultCode == RESULT_OK && data != null && requestCode==0){
        initializeUI(data.getExtras());
    }
    if(resultCode == RESULT_OK && data != null && requestCode==1){
          //data is null here
    }
    super.onActivityResult(requestCode, resultCode, data);
}

My City Activity

 public void addInput(View v){
    Bundle bundle = new Bundle();

    EditText cityBox = (EditText) findViewById(R.id.cityInput);
    String cityName =  cityBox.getText().toString();
    Intent resultIntent = new Intent();
    try {
        EditText longBox = (EditText) findViewById(R.id.longitudeInput);
        String longitude =  longBox.getText().toString();

        double longi =  Double.parseDouble(longitude);

        bundle.putDouble("LONGITUDE", longi);

        EditText latBox = (EditText) findViewById(R.id.latitudeInput);
        String latitude =  latBox.getText().toString();

        double lati =  Double.parseDouble(latitude);

        bundle.putDouble("LATITUDE", lati);

    } catch (NumberFormatException e){

    }
    bundle.putString("CITY_NAME", cityName);
    resultIntent.putExtra("DATA",bundle);
    setResult(RESULT_OK, resultIntent);
    finish(); //calling finish just in case tried without finish aswell
    onBackPressed(); //calling onBackPressed tried without it as well doesn't work 
}

Any help would be appreciated.

try this:

Then, in B, I do:

Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);
finish();

And, in A:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==RESULT_OK && requestCode==1){
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("Date");
}
}

the problem i have found in your code is use of wrong requestCode you are passing 1 as result code in startActivityForResult and in onActivityResult you are checking requestCode==0 means intializeUI etc. All you need to do is use this below code.

     if(resultCode == RESULT_OK && data != null && requestCode==1){
     //everything works fine 
     initializeUI(data.getExtras());
    }

hope this will help you.

I recently faced same issue in my case. What i was doing wrong was that i was using startActivity() by mistake with startActivityForResult() then in child activity i was calling onBackPress() and finish() before setting the result. I can suggest you that you make sure you set the result before finish() the activity and make sure you're starting activity with startActivityForResult() .

Here is snippet from my code.

setResult(RESULT_OK, getIntent().putExtra("country", "some result"));

And i made sure the above written code get excuted before onBackPressed() and onFinish() and it worked for me.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM