onActvityResult still broken? - java

I have an issue where onActivityResult is not being called back in my fragment.
I am starting the activity from my ListFragment as so:
Intent getJobDetailIntent = new Intent(getActivity(),FullJobdetailLayoutActivity.class);
startActivityForResult(getJobDetailIntent,100);
Now I override and set a breakpoint in my fragments onActivityResult,
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
Log.d("ActivityResult FullJobDetailActivity: ", "Called");
}
This is never called in my fragment, onActivityResult is called in my Activity though:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Even letting this call super as above, doesn't work...
I have seen some issues online:
https://code.google.com/p/android/issues/detail?id=15394
And read through a lot of questions on SO regarding this issue....
Does anyone know if it is still a bug?
I need to get data back from the activity started by my fragment!
Thanks all!

Related

StartActivityForResult, but activity finishes

I am trying to use Adobe Image Edit SDK to edit a photo then redirect to another activity, from my custom camera activity.
This works from another activity, simply by creating the Image edit Intent, using startActivityForResult, then treating the "Done" callback in said activity, in the method onActivityResult.
Intent imageEditorIntent = new AdobeImageIntent.Builder(mContext)
.setData(selectedImageUri)
.withToolList(tools)
.withOutput(new File(mLastSavedFilePath))
.build();
startActivityForResult(imageEditorIntent, 2);
and then
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 2) { // i get here
However, when I do this from my Custom Camera Activity, the activity ends when I click "done" in the image edit sdk (its onDestroy is called) before it gets to the result
Intent intent = FileUtils.getInstance().SavePhoto(data, mContext); //this returns an AdobeImageIntent
startActivityForResult(intent, 1);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//this never gets called, because activity finishes, but why?
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
So why is the activity finishing, if the other one does not?
Turns out this was my fault, I did not notice I had android:noHistory="true"
in the manifest for the second activity

onActivityResult requestcode Facebook SDK

I'm building an Android app which uses the Facebook SDKso people can share URL's. I'm using onActivityResult for multiple things in my activity, so I'm using a switch on requestCode so that I know what to do with every activityresult. How do I get the proper requestCode when for example I cancel sharing a facebook post? This is my code at the moment:
import com.facebook.CallbackManager;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
... doing some not-facebook-related stuff here
case 2:
... doing some other not-facebook-related stuff here
// case ??? :
// callbackManager.onActivityResult(requestCode, resultCode, data);
So what is the right requestCode and how do I get this?
This is what I've used in a similar case:
if (requestCode == CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode()) {
// call callbackManager
}
You can also set the offset of requestcodes used by FacebookSdk:
FacebookSdk.sdkInitialize(getApplicationContext(), 10000);
By the way, I have found this looking through the source code, so I'm not sure how "safe" this option is as CallbackManagerImpl.RequestCodeOffset.Login.toRequestCode() is not a "public" documented method.
Try using a sharedialog instead
new ShareDialog(this).registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
#Override
public void onSuccess(Sharer.Result result) {
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
When you cancel share you get to onCancel callback.
Also uncomment this
callbackManager.onActivityResult(requestCode, resultCode, data);
from onActivityResult method.

Custom RecyclerAdapter and startActivityForResult

I have a Fragment that contains a RecyclerView which uses a custom RecyclerAdapter. I have an onClickListener inside my custom RecyclerAdapter - when a position is clicked I want it to start startActivityForResult. So far this works in as such as when it is clicked it starts the Activity as desired. However when I press the back button to go to the Fragment containing the RecyclerView onActivityResult is never called. I have passed in a context to the custom RecyclerAdapter. Is this something that is possible? Or does the Activity/Fragment initiating startActivityForResult be the one that intercepts it? If not I will end up handling the onClick in the Fragment with a gesture detector or something similar, but before that I wanted to give this a fair crack! Note: I have included onActivityResult in the MainActivity which has the Fragment container so the Fragment does receive onActivityResult if startActivityForResult is initiated from the Fragment. My code:
RecyclerAdapter onClickListener:
#Override
public void onClick(View v) {
String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();
Intent intent = new Intent(context, CreateItemsActivity.class);
intent.putExtra("TITLE_ID", titleId);
((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
}
CreateItemsActivity.class - onBackPressed()
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
finish();
}
MyListsFragment.class (contains RecyclerView)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("CALLED", "OnActivity Result");
// if requestCode matches from CreateItemsActivity
if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {
Log.e("REQUEST CODE", String.valueOf(requestCode));
populateRecyclerView();
}
}
Also I have this in the MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// included to allow fragment to receive onActivityResult
}
Your calling Activities onActivityResult will only be called when the second activity finishes and a setResult has been executed.
Since the user is hitting the back button the 2nd activity is finishing without setResult being called.
You'll need to override onBackPressed so you can execute your setResult code.
I see you have implemented this but I think the crux of the issue is that you need to return Activity.RESULT_OK not your request code.
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
super.onBackPressed();
}
In this case you don't need to explicitly return your requestCode of Constants.NEW_ITEMS_REQUEST_CODE because Android will forward that automatically.
OK, so IF by any chance somebody else has this problem here is my
solution. I added this code into the MainActivity onActivityResult (note I have a frame container which is where all fragments are inflated):
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// get current fragment in container
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
fragment.onActivityResult(requestCode, resultCode, data);
}
I believe this works as the MainActivity is top in the hierarchy and intercepts the onActivityResult, so basically I just point it where I want it to be used.

Android Studio Facebook: onActivityResult in normal class

In order to listen to a facebook login event I must override in my activity the function onActityResult and make a call to facebook's callbackmanager.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
So far everything's working.
I'd like to create a static class for facebook that handles facebook callbacks, such as login, etc.
Is there substitute for onActivityResult in that case?
You mean you want another class to handle the activity returning? Then you have to have onActivityResult of the activity that calls startActivityForResult call into your facebook class. There's no way to reroute it automatically.

Using multiple intents in same Activity

I am trying to use an intent to import a file form the device to my app. I am using Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
My question is... I know for the intent I need to have onActivityResult() method, but I already have one for another intent being used the the class, is there a way I can have two onActivityResult() methods for two intents.
onActivityResult(int requestCode, Result resultCode, Intent data)
use differnet requestCode for different intents
startActivityForResult(intent, requestCodeForIntentOne);
startActivityForResult(intent, requestCodeForIntentTwo);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode==requestForIntentOne)
{}
if(requestCode==requestForIntentTwo)
{}
}
when you call startActivityForResult(), you supply a requestCode. That will match the value of the same name you get in onActivityResult()
So you can do :
if (requestCode == requestCodeA) {
//handle case 1
} else if (requestCode == requestCodeB) {
//handle case 1
}

Categories

Resources