LittleSquinky
LittleSquinky

Reputation: 569

onActivityResult in fragment is never called

I'm calling an activity from a fragment with startActivityForResult, however onActivityResult is never called back. I have looked for possible solutions, including removing SingleTask, SingleInstance, NoHistory from the manifest, but nothing worked so far.

This is the callee fragment:

public class PickUpButtonFragment extends Fragment {
....
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_pick_up_button, container,
                false);

        ...
        pickUpButton = (Button) view.findViewById(R.id.pickUpButton);
        pickUpButton.setOnClickListener(new OnPickupClickListener());
...
        return view;
    }
....
    private class OnPickupClickListener implements OnClickListener {
        @Override
        public void onClick(View view) {
            Intent qrCodeActivityIntent = new Intent(getActivity(), QrCodeActivity.class);
            startActivityForResult(qrCodeActivityIntent, 20);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == 20) { 
            qrCodeValue = data.getStringExtra(QrCodeActivity.QR_CODE_FLAG);
            requestDeviceLocation();
        }
    }
...
}

The fragment is included in an activity in which I overrode the onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
}

This is how the activity is declared in the manifest:

<activity
    android:name=".activity.GMapActivity"
    android:label="@string/map_title"
    >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

The called activity has a setResult method:

public class QrCodeActivity extends Activity {

...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_code);
.....   
        initPickUpRequestButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent data = new Intent();
                data.putExtra(QR_CODE_FLAG, qrCodeValue);
                setResult(RESULT_OK,data);
            }
        });

...
    }
....
}

This is how the called activity is declared in the manifest:

<activity
    android:name=".activity.QrCodeActivity"
    android:label="@string/qr_code_title"
    >
</activity>

setResult is called, but onActivtyResult is never called back, both in the parent activity and in the fragment.

I'm building with 4.1.2.

Upvotes: 0

Views: 322

Answers (1)

StoneBird
StoneBird

Reputation: 1940

You need to call finish() to end your activity after your setResult() in order for any caller to receive the result.

If you only want to send message back to caller without ending this activity then you probably want to use callback patterns or broadcast.

Upvotes: 1

Related Questions