Branden Taylor Branden Taylor - 3 years ago 159
Android Question

When onActivityResult is ran resultCode is 0 and data is null

I have an activity that sends a boolean array from my inventory class back to my mainGame activity. But when onAcivityResult runs resultCode is 0 and my data is null.

starting the Inventory activity:

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void openInventory(View V) {
Intent bag = new Intent(MainGame.this, Inventory.class);
bag.putExtra(booInventory, inventory);
startActivityForResult(bag, requestItem);

ending the Inventory activity:

public void onPause(){
Intent Item = new Intent(Inventory.this, MainGame.class);//data sent to MainGame activity
Item.putExtra(tools, itemUsed);
setResult(RESULT_OK, Item);

onActivityResult: (toaster is a method that creates toast)

public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == 2){
if(resultCode == RESULT_OK){
toaster("result is ok", 500);

if(data != null){
toaster("it worked", 1500);
toaster("FAIL!", 1500);

Answer Source

Firstly, you shouldn't do that in onPause().

Secondly, replace that

Intent Item = new Intent(Inventory.this, MainGame.class) 

with this:

Intent returnIntent = new Intent();

Bonus: Use static variables for the code such as:

private static int SECOND_ACTIVITY_REQUEST_CODE = 2;

and use that variable inside your methods instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download