how to use onActivityResult in android more than one - android

enter code here public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//If signin
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
//Calling a new function to handle signin
handleSignInResult(result);
}
// this is for facebook
callbackManager.onActivityResult(requestCode, resultCode, data);

See developers guide http://developer.android.com/training/basics/intents/result.html
It depends upon your request code you give when assigning startActivity(intent,request_Code),you can have multiple actions performed.

Related

onActivityResult not getting called in fragment

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);
}

Firebase android auth, how to distinguish between login and register

As we receive the login response onActivityResult as below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == RC_SIGN_IN) {
handleSignInResponse(data);
return;
}
Timber.d(getString(R.string.unknown_response), requestCode);
return;
}
How to distinguish whether it is new registered user or login user ?

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()

Is there any callback method in twitter using fabric in android after sharing the post?

I have used the following code to post the tweet in twitter,for the custom login button
File file = captureScreen();
TweetComposer.Builder builder = new TweetComposer.Builder(getActivity())
.text("just setting up my Fabric.")
.image(Uri.fromFile(file));
builder.show();
sharing the post with image is working fine, is there any call back method to show alert to user in success.
Thanks in advance.
Use the code below ->
Intent i = new TweetComposer.Builder(this)
.text(content)
.url(new URL(url))
.createIntent();
startActivityForResult(i, Constants.REQUEST_CODE_TWITTER);
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_CODE_TWITTER) {
if (resultCode == RESULT_OK) {
// do your stuff
}
}
}

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.

Categories

Resources