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()
Related
I have an App with 2 Activities. Activity A (parent) starts Activity B (child) by calling:
startActivityForResult(intentB,B_Request);
Activity B is a barcode scanner. (I use this library: com.journeyapps:zxing-android-embedded:3.2.0#aar).
In this activity I have to override the onActivityResult() method in order to get the scanned code. After that I want to send the data stored in 'code' back to the parent (Activity A) onActivityResult(). I did this like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String code = null;
if(result != null){
if(result.getContents() == null){
return;
}
else {
code = result.getContents();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
Intent intent = new Intent(getApplicationContext(),A.class);
intent.putExtra("scannedCode", code);
setResult(RESULT_OK, intent);
finish();
}
The parent onActivityResult() looks like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
String scannedCode = data.getStringExtra("scannedCode");
toastMessage("Scanned code: " + scannedCode);
}
}
}
I get the following exception and instead of seeing Activity A, I'm redirected to the Activitie's A parent.
E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:24782)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24902)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24879)
at com.android.server.am.ActivityStackSupervisor.ensureVisibilityAndConfig(ActivityStackSupervisor.java:1671)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1420)
at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1709)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:3043)
at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2488)
at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:2234)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1745)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1669)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:9657)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:224)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
at android.os.Binder.execTransact(Binder.java:752)
p.s. It's not enough for me to get the code in Activity B, I need the code back in Activity A.
Can you please tell me what am I doing wrong here?
Thank you!
just put new intent() instead of
new Intent(getApplicationContext(),A.class)
in your child activity
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);
}
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);
}
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.
This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 8 years ago.
I am new to android development
I call an intent now how can i get result from called activity
can any one tell me how to perform this task ?
i have called intent like.
Intent I = new Intent (this ,abc.class);
startActivity(i);
thanks
Use startActivityForResult and then override onActivityResult in your FirstActivity.
In FirstActivity
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
Override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
Log.i("Message is",message);
// logs Testing
}
}
In SecondAcivity
Intent intent=new Intent();
intent.putExtra("MESSAGE","Testing");
setResult(2,intent);
finish();//finishing activity
Reference to the docs:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)
Example:
http://www.javatpoint.com/android-startactivityforresult-example
For going to second activity use startActivityForResult in your firstclass
Intent callIntent = new Intent(FirstClass.this, SecondClass.class);
startActivityForResult(callIntent, 1);
then override onActivityResult method in your first class like this
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// get values from data
}
} }
In your second class do this for returning back, if you want to send
something to first class. Store this in your intent.
Intent result = new Intent(); setResult(Activity.RESULT_OK, result);
finish();
hi you can get result calling activity
Start activity with startActivityForResult(intent, 0);
add below method in calling activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// do your task
} else if (resultCode == RESULT_CANCELED) {
// do your task
}
}
}
In your main Class....
static final int CODE_REQUEST = 1; // The request code
....
Intent pickContactIntent = new Intent(MainClass.this, CallingClassName.class);
startActivityForResult(pickContactIntent, CODE_REQUEST);
..........
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
In your calling class
Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
finish()