Wrong requestCode in onActivityResult
You are calling startActivityForResult()
from your Fragment
. When you do this, the requestCode
is changed by the Activity
that owns the Fragment
.
If you want to get the correct resultCode
in your activity try this:
Change:
startActivityForResult(intent, 1);
To:
getActivity().startActivityForResult(intent, 1);
Wrong requestCode returned onActivityResult from another Activity
Just replace
startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);
with
getActivity().startActivityForResult(intent, Defines.FILTER_BY_CATALOGUE);
It will work for sure. :)
why am I getting wrong requestCode?
It happens because if you use startActivityForResult
from the dialog, in onActivityResult
you get the requestCode of the dialog. Not from your another Activity. And if you want to get right requestCode, you should use getActivity().startActivityForResult(intent,requestCode)
in DialogFragment.
onActivityResult not passing requestCode
you have messed request code and result code in onActivityResult
, in first line you are comparing request and result, these won't never be ==
(or will always be if IMAGE_PICK_GALLERY_CODE
is set to -1
like RESULT_OK
is)
if (requestCode == RESULT_OK) {
if (resultCode == IMAGE_PICK_GALLERY_CODE) {
some camera/gallery apps may return wrong result code, still returning also data/images, so checking requestCode
and proper payload in data
Intent
should be sufficient
if (requestCode == IMAGE_PICK_GALLERY_CODE && data!= null) {
imageUri = data.getData();
if (imageUri != null)
...rest of code
else if (requestCode == IMAGE_PICK_CAMERA_CODE) {
...
Wrong result code after onActivityResult() to enable bluetooth and discovery
Value of resultCode is same as discoverable duration EXTRA_DISCOVERABLE_DURATION
passed in Intent.
and by default duration is 120. So it is OK.
If you will start activity like this
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
startActivityForResult(i,DISCOVER_REQUEST);
It will return 240 as result code.
onActivityResult in wrong activity is being called
I was overriding the onStop method like this:
@Override
public void onStop(){
setResult(RESULT_CANCELED, new Intent());
finish();
}
Basically, I didn't understand how the Android Lifecycle worked. I thought onStop() would only be called when an Activity was KILLED, and I thought it wouldn't be killed if it was waiting for a response from another Activity.
So what was happening was this:
Activity C launched Activity A, which launched Activity B. When Activity B started, A.onStop() was called. Then, when Activity B finished, it (correctly) tried to return back to Activity A, but Activity A was already Stopped, so it returned all the way back to Activity C.
Error on the super.onActivityResult() why?
First of all, if you get an error - always show logcat. Secondly, if this method is declared in the class extending Activity
, there is no need to call super
there since this method is empty in the Activity
class:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
Related Topics
Gcm Service_Not_Available on Android 2.2
Firebase Auth Using Phone Number and Password
How to Send File Using Bluetooth on Android Programatically
How to Handle Ontouch Event for Map in Google Map API V2
Android: Disable Soft Keyboard at All Edittexts
Android - How to Enable Autostart Option Programmatically in Xiaomi Devices
How to Send JSON Object to Server Using Volley in Android
How to Get the Fragment Instance from the Fragmentactivity
Android Path to Asset Txt File
Android: Determine Active Input Method from Code
Not Able Disable Home Button on Specific Android Devices
Adb Server Is Out of Date. Killing
Starting Frame-By-Frame Animation
Icon in Menu Not Showing in Android
How to Join Png with Alpha/Transparency in a Frame in Realtime
Android Studio 3.0 Execution Failed for Task: Unable to Merge Dex