Google Play Games Achievements onClose callback - java

Currently we open Achievements View with
startActivityForResult(mHelper.getGamesClient().getAchievementsIntent(),
REQUEST_ACHIEVEMENTS);
Can we get some kind of callback when achievements view was closed? Or if it is possible - to get a callback when user Exits GPG within the achievements view. GameHelper's "onDisconnected()" is not firing for some reason - when that happens.
I've googled this - but found nothing
Edit: should i look for some specific responseCode inside onActivityResult
?

Declare this variable and this method in your class.
private static int achievementsIntent = 10001;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == achievementsIntent) {
//you are in your callback
if (resultCode == GamesActivityResultCodes.RESULT_RECONNECT_REQUIRED) {
// user logged out from achievements screen
}
}
}
Then call your achievements intent:
startActivityForResult(Games.Achievements.getAchievementsIntent(GoogleApiClient), achievementsIntent);

Related

How to get setup result from stripe in a Fragment?

How do i get the response from stripe with the new Activity Result Api? I create a new card and i want to attach it to a stripe account. To do that i have to use the confirm setup intent and wait for the answer from stripe. But how do i register this one with the new Api? This is my code and but the onActivityResult has been deprecated now. I know how to use it for picking images or making phone calls and other that can use the launch function of the register activity. I don't want to do this on the server side because if the card needs 3D secure webview stripe handles that automatically.
ConfirmSetupIntentParams confirmParams = ConfirmSetupIntentParams
.create(paymentMethodParams, clientSecret);
stripe.confirmSetupIntent(this, confirmParams);
#Override
public void onActivityResult(int requestCode, int resultCode, #org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
stripe.onSetupResult(requestCode, data, new ApiResultCallback<SetupIntentResult>() {

Why doesn't startActivityForResult() lead to onActivityResult() execution?

Still trying to make my screen record app. I keep on working with MediaRecorder, as I was told some time ago, so I got stuck with another problem.
I just need to initialize a MediaProjection object to make my code work, that's what I do in onActivityResult(), as it's written in this guide:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECORD_REQUEST_CODE && resultCode == RESULT_OK) {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
screenRecorder.setMediaProject(mediaProjection);
}
}
The setMediaProjection() looks like
public void setMediaProject(MediaProjection project) {
mediaProjection = project;
}
, so it shouldnt't cause any trouble.
And that's how I try to call onActivityResult():
/* start transmission */
if(screenRecorder.isRunning()) {
screenRecorder.stopRecord();
} else {
Intent captureIntent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(captureIntent, RECORD_REQUEST_CODE);
}
The fun and crazy thing is that when I first launched the debug, it worked! After startActivityForResult() I got to onActivityResult() and initialized mediaProjection: my phone showed me a dialog window whether I allow to capture the screen or not, so I allowed that and got a special symbol (smth like screen with displayed waves) at my status bar.
But a few moments later I found an issue when stopping the record and restarted the debug session to trace it more exactly. After that onActivityResult() is just ignored: startActivityForResult() is called, the dialog window is shown, but after allowing the record onActivityResult() is completely skipped and mediaProjection is null. The restarting and re-installing the apk with the same code didn't fix anything.
Thank you very much for any suggestions.
Your onActivityResult only does something when the result code is OK, try this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECORD_REQUEST_CODE && resultCode == RESULT_OK) {
mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
screenRecorder.setMediaProject(mediaProjection);
} else {
//TODO: Do something
Toast.makeText(getBaseContext(), "Result code is not RESULT_OK, ", Toast.LENGTH_LONG).show();
}
There was a very simple solution. I just initialized the record service that put null into mediaRecorder's mediaProjection, so after that I was unable to re-initialize it. Putting intent which called onActivivtyResult() into activity's onCreate() before starting the service fixed that.
I got same problem. This could bu about threading. In my project I started an activity and opening activity try to start chrome intent. Chrome is opening in debug mode but not opened real mode.
I added a delay when opening chrome
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
startAuth();
}
}, 100);

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.

StartActivityForResult - check for existence of intent extra or use custom result code?

I have a search button in MainActivity which launches SearchActivity. In SearchActivity, the user can either choose from one of the predefined categories listed, or can enter in a search query. The SearchActivity will return a different extra depending on which way the user searches. The code that runs in MainActivity will depend on which extra is returned.
I'm debating which way is better or more "correct". I've coded it both ways, and it works either way.
The first way I coded it was to check for the existence of the intent extra:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
if (data.getExtras().containsKey("extra1")) {
extra1 = data.getStringExtra("extra1");
}
if (data.getExtras().containsKey("extra2")) {
extra2 = data.getStringExtra("extra2");
}
}
// plus rest of code for checking for RESULT_CANCELED
}
}
The other way I coded it is by using custom result codes. The result codes RESULT_OK_CATEGORY and RESULT_OK_SEARCH are public static variables in MainActivity so that they can be accessed from SearchActivity, and sent back as a result code through setResult(MainActivity.RESULT_OK_CATEGORY, intent) or setResult(MainActivity.RESULT_OK_SEARCH, intent) respectively.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SEARCH_REQUEST_CODE) {
if (resultCode == RESULT_OK_CATEGORY) {
extra1 = data.getStringExtra("extra1");
} else if (resultCode == RESULT_OK_SEARCH) {
extra2 = data.getStringExtra("extra2");
}
// plus rest of code for checking for RESULT_CANCELED
}
}
Which way is better, and why? Checking for the existence of the extra, or checking for a custom result code?
You should use resultCode because it's completely under your control. You may lose values set in Intent's extras, as revealed in this question (referenced by Ajay in the comments).
Note, if you use many custom result codes, it has been recommended to use a switch statement for code clarity.

There is no onActivityResult() in Fragment. For in-app Billing

I am following the tutorial and code examples from Google on how to implement in-app Billing. The thing is that I'm doing this in a Fragment.
Up until the last step everything seem to implement alright, but then I'm supposed to implement this method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
if (mHelper == null) return;
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
And the problem is of course that this protected method doesn't exist in the fragment. There is only a public method, that doesn't get called when finishing a in-app purchase.
a simpler solution can be this
create a method in main activity
set the mhelper instance from fragment to main activity with that method
and use that instance in onActivity result

Categories

Resources