onActivityResult not getting called in fragment - android

code in fragment
private static final int BRAINTREE_REQUEST_CODE = 777;
BottomMenu fragment = this;
public void onBraintreeSubmit(String clienTtoken) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(getActivity()), BRAINTREE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
Toast.makeText(getActivity(), result.getPaymentMethodNonce().getNonce() + "", Toast.LENGTH_SHORT).show();
// use the result to update your UI and send the payment method nonce to your server
sendPaymentNonceToServer(result.getPaymentMethodNonce().getNonce());
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}
code in activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Problem:
I have implemented BrainTree payment gateway with onBraintreeSubmit() method I startActivityResult and catch it onActivityResult but it's not being called in the fragment.
onActivityResult only gets called if I cancel payment but never when payment success or exception thrown

First you have to call from activity
Add below code in your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
fragment.onActivityResult(requestCode, resultCode, data);
}

If you call startActivityForResult() with activity instance, then onActivityResult will get in your activity not in fragment. This is the reason for your issue.
Modified the code as below. This will solve your issue.
public void onBraintreeSubmit(String clienTtoken, BottomMenu context) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(context.getContext()), BRAINTREE_REQUEST_CODE);
}

Related

Use onActivityResult when button IsClicked

I have an activity that once a ToggleButton is clicked, it starts another activity as follows:
Intent intent = new Intent(ChatActivity.this, BBActivity.class);
startActivityForResult(intent,1);
In that second activity it performs few calculations and then:
confirmedData.putExtra( "confirmedB", (Serializable) bSelected );
confirmedData.putExtra( "dateInMillis",DateInMillis );
setResult(ChatActivity.RESULT_OK,confirmedData);
Then, I use the following to get the results in my first activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
ArrayList<DiscoverB> confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
Now, I had like to use confirmedB Once a button in my activity is clicked.
How can I pass the values from onActivityResult to my ClickListener?
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
HERE I NEED MY confirmedB
}
};
Thank you
In your first activity, declare a variable
ArrayList<DiscoverB> confirmedB;
In OnActivityResult, you can initialize confirmedB
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
You can use it now in your clickListener,
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
here you can use confirmedB
}
};

How to pass variable to onActivityResult() from populateViewHolder?

I have a Firebase Recycler View which contains a button like this
final String post_key = getRef(position).getKey();
viewHolder.btnUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("post_key",post_key);
Log.d("post_key", data.getExtras().getString("post_key"));
data.setType("image/*");
data.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(data, GALLERY_REQUEST);
}
});
This will trigger an action in OnActivityResult(). I was trying to access the variable "post_key" in OnActivityResult. Therefore, I put it to the extra, and call the getString function in OnActivityResult.
final String post_key=data.getExtras().getString("post_key");
However, the app crashed and keeps telling me that I attempt to invoke virtual method 'getString' on a null object reference
You need to check the request code and the result code as well:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
// here you can access the intent
String msg = (data != null) ? data.getStringExtra("post_key") : "";
Log.d("post_key", "Got post_key: " + msg);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
You can not send your data to another app - if it does not accept. The thing you are doing is not mentioned anywhere.
Correct Way
in onClick don't pass extra, and hold your post_key in your activity/ adapter.
adapter.setPostKey(post_key);
Then in onActivityResult() get this post_key.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
String post_key = adapter.getPostKey();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

Not receiving callback in onActivityResult() after sending invite request to Google plus

not receiving callback in onActivityResult() after sending invite request to Google plus
public void sendGooglePlusInvite(){
Intent intent = new AppInviteInvitation.IntentBuilder(getActivity().getString(R.string.app_name))
.setMessage(getActivity().getString(R.string.invite_message_less))
.setCustomImage(Uri.parse(getActivity().getString(R.string.app_image_url)))
.build();
getActivity().startActivityForResult(intent, REQUEST_INVITE);
}
on call back in
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == REQUEST_INVITE) {
// Get the invitation IDs of all sent messages
String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
for (String id : ids) {
Log.d("TAG", "onActivityResult: sent invitation " + id);
}
}
} else {
// Sending failed or it was canceled, show failure message to the user
// ...
}
}
Please help me
Yes, you can't receive callback by this way:
To get the result in your fragment make sure you call startActivityForResult(intent,REQUEST_INVITE); instead of getActivity().startActivityForResult(intent,REQUEST_INVITE); inside your fragment.
And secondly make sure you implement this method in your activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
this will call your fragment's onActivityResult()

How to handle Twitter and facebook request code

How to handle Twitter and facebook request code On ActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// If request code of facebook than i will call other function and if
// request code of twitter than i will call some other function but
// how i can seprate both by request code.
}
As of com.twitter.sdk.android:twitter:1.14.1 you can use:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TwitterAuthConfig.DEFAULT_AUTH_REQUEST_CODE) {
// Twitter request code
// TwitterLoginButton::onActivityResult(requestCode::int, resultCode::int, data::Intent);
} else {
// Use Facebook callback manager here
// CallbackManager::onActivityResult(requestCode::int, resultCode::int, data::Intent);
}
}
You Have 2 options :
1) On click of fb or twitter button, set a Boolean value to true and check which button was clicked to determine the method you want to call.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (fb_clicked == true)
{
//call the callback manager's onActivityResult
}
else if(twitter_clicked == true)
{
//call the twitter login button's onActivityResult
}
}
2) Or you can Use the Auth Token to determine which button was clicked as the auth token will be generated when you click on the LoginButton (twitter or facebook and the other should be null)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TwitterAuthConfig.DEFAULT_AUTH_REQUEST_CODE) {
// Twitter request code
// TwitterLoginButton::onActivityResult(requestCode::int, resultCode::int, data::Intent);
} else {
// Use Facebook callback manager here
// CallbackManager::onActivityResult(requestCode::int, resultCode::int, data::Intent);
}
}
EDIT : Updated the code with Patrick W's answer for the 2nd option, which is a much better approach and as he mentions this solution works for com.twitter.sdk.android:twitter:1.14.1 and above.

Android onActivityResult in v4 Fragment not worked

I try onActivityResult in fragment currently I am using v4 fragment.
my activity code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v(tag, "0000");
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
My fragment code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v(getTag(), "in 0");
if (resultCode == Activity.RESULT_OK) {
Log.v(getTag(), "in 1");
if (requestCode == MY_INTENT_CLICK) {
Log.v(getTag(), "in 2");
if (null == data)
return;
Log.v(getTag(), "in 3");
Uri selectedImageUri = data.getData(); } } }
I call start Intent in
Log.v(getTag(), "in 00");
Intent intent = new Intent();
Log.v(getTag(), "in 01");
intent.setType("video/*");
Log.v(getTag(), "in 02");
intent.setAction(Intent.ACTION_GET_CONTENT);
Log.v(getTag(), "in 03");
fragment.startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
I also try call intent in
getActivity().startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
But noting is work. I also refer.
Fragment Compatability onActivityResult() Not Working
onActivityResult is not being called in Fragment
onActivityResult() not called in new nested fragment API
Refer below code
public class RegisterDeRegisterResponse {
private Vector<IResultResponse> _iCallBack = new Vector<IResultResponse>();
public static RegisterDeRegisterResponse ref = null;
public static RegisterDeRegisterResponse getInstance() {
if (ref == null) {
ref = new RegisterDeRegisterResponse();
}
return ref;
}
/**
* Register to recieve server response.
*/
public void registerForServerResponse(IResultResponse callback) {
_iCallBack.addElement(callback);
}
/**
* Notify all registered users about server response with corresponding
* process id.
*/
public void notifyRegisteredUser(int requestCode, int resultCode,
Intent data) {
Enumeration<IResultResponse> enumeration = _iCallBack.elements();
while (enumeration.hasMoreElements()) {
IResultResponse callback = enumeration.nextElement();
callback.onActivityResult(requestCode, resultCode, data);
}
}
/**
* Deregister to recieve server response
*/
public void deRegisterForServerResponse(IResultResponse callback) {
_iCallBack.removeElement(callback);
}
}
And also make Interface which have onActivityResult method declaration
public interface IResultResponse {
public void onActivityResult(int requestCode, int resultCode, Intent data);
}
Now in class in which you are calling startActivityForResult put this line before startActivityForResult and implement IresultResonse also
RegisterDeRegisterResponse register1 = RegisterDeRegisterResponse
.getInstance();
register1.registerForServerResponse(this);
and also deregister in onActivityResult
RegisterDeRegisterResponse
.getInstance().deRegisterForServerResponse((IResultResponse)this);

Categories

Resources