Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment Compatibility onActivityResult() Not Working

I have been working on an application for android that utilizes the android comparability library (android-support-v4). Before SDK 20, I was able to compile my application with the following uses-sdk entry in my manifest.

<uses-sdk android:minSdkVersion="7" />  

All my fragments that started an activity for result received their results properly.

One day (Before I updated to SDK 20) I was fixing lint issues, and I added android:targetSdkVersion to the manifest per the lint flag's request, and I soon realized that none of my fragments were receiving their onActivityResilt() calls.

Now every since I have updated to SDK 20.0.1, I am forced to utilize the android:targetSdkVersion in uses-sdk entry in my manifest; otherwise R.java is never generated.

Here is my current uses-sdk manifest entry

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="16" />

Currently because of this odd bug, my application is currently still broken. I have tryied updating my compatibly library the the latest version r9.

Also I have double checked all my startActivityForResult() calls in the fragments. They all use the proper calls through the fragment's method.

i.e.

 this.startActivityForResult(intent, requestCode);

not

 this.getActivity().startActivityForResult(intent, requestCode);

My current Development Environment:

Android SDK 20.0.1
Android SDK Platform Tools 13
ADT 20.0.2

Any help would be greatly appreciated, as this bug is currently the brick wall that is preventing my final beta tests.

EDIT

Here's my output of ant debug

$ ant debug Buildfile: /home/ricky/Development/workspace/location_ringer/LocationRinger/build.xml

-set-mode-check:

-set-debug-files:

-check-env: [checkenv] Android SDK Tools Revision 20.0.1 [checkenv] Installed at /app/android-sdk-linux_86

-setup: [echo] Project Name: ListActivity [gettype] Project Type: Application

-set-debug-mode:

-debug-obfuscation-check:

-build-setup: [echo] Resolving Build Target for ListActivity... [gettarget] Project Target: Google APIs [gettarget] Vendor: Google Inc. [gettarget] Platform Version: 4.1 [gettarget] API level:
16 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for ListActivity... [dependency] Library dependencies: [dependency] [dependency] ------------------ [dependency] Ordered libraries: [dependency] [dependency] ------------------ [dependency] API<=15: Adding annotations.jar to the classpath. [echo] ---------- [echo] Building Libraries with 'debug'...

nodeps:

-set-mode-check:

-set-debug-files:

-check-env: [checkenv] Android SDK Tools Revision 20.0.1 [checkenv] Installed at /app/android-sdk-linux_86

-setup: [echo] Project Name: LocationLib [gettype] Project Type: Android Library

-set-debug-mode:

-debug-obfuscation-check:

-build-setup: [echo] Resolving Build Target for LocationLib... [gettarget] Project Target: Google APIs [gettarget] Vendor: Google Inc. [gettarget] Platform Version: 4.1 [gettarget] API level:
16 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for LocationLib... [dependency] Library dependencies: [dependency] No Libraries [dependency] [dependency] ------------------ [dependency] API<=15: Adding annotations.jar to the classpath.

-pre-build:

-code-gen: [mergemanifest] No changes in the AndroidManifest files. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] No changed resources. R.java and Manifest.java untouched. [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] No need to generate new BuildConfig.

-pre-compile:

-compile: [echo] Creating library output jar file...

-post-compile:

-obfuscate:

-dex: [echo] Library project: do not convert bytecode...

-crunch: [crunch] Crunching PNG Files in source dir: /home/ricky/Development/workspace/location_library/LocationLib/res
[crunch] To destination dir: /home/ricky/Development/workspace/location_library/LocationLib/bin/res [crunch] Crunched 0 PNG files to update cache

-package-resources: [echo] Library project: do not package resources...

-package: [echo] Library project: do not package apk...

-post-package:

-do-debug: [echo] Library project: do not create apk... [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/location_library/LocationLib/bin/build.prop

-post-build:

debug:

nodeps:

-set-mode-check:

-set-debug-files:

-check-env: [checkenv] Android SDK Tools Revision 20.0.1 [checkenv] Installed at /app/android-sdk-linux_86

-setup: [echo] Project Name: ExceptionHandlerLib [gettype] Project Type: Android Library

-set-debug-mode:

-debug-obfuscation-check:

-build-setup: [echo] Resolving Build Target for ExceptionHandlerLib... [gettarget] Project Target: Android 4.1 [gettarget] API level:
16 [echo] ---------- [echo] Creating output directories if needed... [echo] ---------- [echo] Resolving Dependencies for ExceptionHandlerLib... [dependency] Library dependencies: [dependency] No Libraries [dependency] [dependency] ------------------ [dependency] API<=15: Adding annotations.jar to the classpath.

-pre-build:

-code-gen: [mergemanifest] No changes in the AndroidManifest files. [echo] Handling aidl files... [aidl] No AIDL files to compile. [echo] ---------- [echo] Handling RenderScript files... [renderscript] No RenderScript files to compile. [echo] ---------- [echo] Handling Resources... [aapt] No changed resources. R.java and Manifest.java untouched. [echo] ---------- [echo] Handling BuildConfig class... [buildconfig] No need to generate new BuildConfig.

-pre-compile:

-compile: [echo] Creating library output jar file...

-post-compile:

-obfuscate:

-dex: [echo] Library project: do not convert bytecode...

-crunch: [crunch] Crunching PNG Files in source dir: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/res [crunch] To destination dir: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/res [crunch] Crunched 0 PNG files to update cache

-package-resources: [echo] Library project: do not package resources...

-package: [echo] Library project: do not package apk...

-post-package:

-do-debug: [echo] Library project: do not create apk... [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop [propertyfile] Updating property file: /home/ricky/Development/workspace/exception_handler_library/ExceptionHandlerLib/bin/build.prop

-post-build:

debug:

-pre-build:

-code-gen: [mergemanifest] Merging AndroidManifest files into one. [mergemanifest] Merging manifests from project and 2 libraries. [mergemanifest] Warning: [AndroidManifest.xml:3, AndroidManifest.xml:3] Main manifest has but library uses targetSdkVersion='16' [mergemanifest] Note: main manifest lacks a declaration, which defaults to value minSdkVersion or 1. [mergemanifest] Warning: [AndroidManifest.xml:3, AndroidManifest.xml:13] Main manifest has but library uses targetSdkVersion='16' [mergemanifest] Note: main manifest lacks a declaration, which defaults to value minSdkVersion or 1.

BUILD FAILED /app/android-sdk-linux_86/tools/ant/build.xml:616: null

Total time: 2 seconds

EDIT

I looked into /app/android-sdk-linux_86/tools/ant/build.xml:616 and found that the following line threw the null enabled="${manifestmerger.enabled}">

here is the entire block

I am going to try to disable the manifest merger option an see what happens

EDIT

It would seem that removing manifestmerger.enabled=true from project.properties has solved my problem. The project now compiles and works perfectly. I think I should report this bug to the android team. My manifest has the following entry

<uses-sdk android:minSdkVersion="7"  />

However onActivityResult() still isn't called if I raise the targetSdkVersion higher than 7, so this doesn't really solve the root problem, but my project is now working properly

like image 295
Rick Barrette Avatar asked Jul 30 '12 16:07

Rick Barrette


People also ask

Can we use onActivityResult in fragment?

We can call startActivityForResult directly from Fragment but actually mechanic behind are all handled by Activity. Once you call startActivityForResult from a Fragment, requestCode will be changed to attach Fragment's identity to the code.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

What can I use instead of startActivityForResult?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.


2 Answers

In Fragment (Wrong startActivityForResult)

getActivty().startActivityForResult(intent, requestCode); 
// Cannot receive in fragment

In Fragment (Correct startActivityForResult)

startActivityForResult(intent, requestCode); 
// use Fragment.startActivityForRusult()
// not Activity.startActivityForRusult()

In activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

In Fragment

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Do Action
}
like image 95
ChangUZ Avatar answered Nov 15 '22 07:11

ChangUZ


Here's a couple of suggestions:

  • If you just want to be able to not have android:targetSdkVersion in your manifest, you should be able to right-click your project in the Project Explorer in Eclipse and select Android Tools > Clear Lint Markers.

  • Check to make sure that all your Fragment and Activity-derived classes extends the correct classes from the compatibility library (android.support.v4.app.Fragment and android.support.v4.app.FragmentActivity) and none are accidentally extending the ones in the framework (android.app.Fragment and android.app.Activity).

Also, I find it strange that your project doesn't compile if you set your targetSdkVersion to anything other than 16. Is there any specific issue indicated in the Problems/Console/Lint window? Please also check whether you have other target available and/or selected as Project Build Target (right-click project, Properties > Android).

like image 2
Joe Avatar answered Nov 15 '22 09:11

Joe