Using the Google Play Location and Settings API a developer can easily check if the Android device's location settings are correct.
The API also supports fixing any settings that are not correct using the startResolutionForResult method. (It calls Activity.startActivityForResult() and provides feedback of users "yes/no" choice in the activities onActivityResult() method.)
This all works fine so far, except that I am calling startResolutionForResult from a fragment. As per this question, an intent needs to called via Fragment.startActivityForResult() for the result to be given to the fragment as well.
Is there any way of getting this result to the fragment? I was half expecting a createIntentForResolutionForResult on the ResultStatus class. Ideally I don't want to modify my containing Activity.
You need to call super.onActivityResult on the Activity and call your Fragment from there. Otherwise its presence is there just for the sake of it and no work is done on the calling method which is startResolutionForResult in this case. Check the source of FragmentActivity.
Related
I am restructuring my code to use ActivityResultContracts, and I've been able to recreate the old way of doing things with onActivityResult by passing in "codes" with my Intents and just passing them back from the called activity. Everything is working fine, but how would I manage to do that with App Updates, even the Google documentation is still using onActivityResult to check for app updates.
After looking at it for the past few days, it does not seem like it is possible to forgo using onActivityResult; the cancel response is only returned to that method. If you have a listener setup to check for resumed downloads, you will get a response there (as onResume is called when the calling activity is moved back to the foreground) but you only get that an update is available and the install status is unknown (which coincidentally is the same value as cancelled, and you can't do anything there as you get the same values on the initial call to check for an update). I don't see the point in deprecating a method you're still required to use, but oh well.
After reading many SO threads and answers about proper Activity-Fragment implementation for long running tasks, I currently have an activity adding a UI less Fragment to it thru a FragmentTransaction and letting it run on its own retaining its instance state.
But the problem seems to start off when the "Google Drive Android API" object's Connect method is called (thereby causing a onConnected call) and then the activity going thru a restart due to screen rotation or so.
Now, having a fragment handling the drive mechanism, it causes the fragment to work properly, but many of the drive api calls require a CONTEXT (in the form of activity) to tie back to.
As a result, at a given fraction of time, when the activity is being restarted and fragment is being detached and reattached, there is no guarantee that the getActivity() call would return a correct activity context.
The drive api (inside the onConnected or within some other result callbacks) thereby fail, as they don't have an activity to tie back to.
How to handle such a scenario? Is there a way to tell the drive api to wait while the activity is being recreated?
I can post my code, if that helps, but think this is a pretty generic scenario in handling screen rotations when using drive api for android.
I am new to android programming. I am planning to make an app that is location sensitive.
I am trying to use Google Play Services' Location Based APIs to detect the user's current location. For this I have been following the sample code and the details given here :-
developer.android.com- detect user's current location- Location APIs- Google Play Services
I am trying to understand the connection between the different method calls involved in detecting a user's current location. Now the above link although seems to have a lot of information, being a newbie, I find it difficult to connect the different method calls here.
So roughly what I understand form the code in the above URL is this :-
In the onCreate() of the MainActivity class we create a LocationClient. LocationClient is used to connect to the location services.
Now in the onStart() inside the MainActivity Class, we call locationClientObjet.connect().So it means that whenever the MainActivity of the app becomes visible, an attempt to connect to the location services is made.
Now before we even made the LocationClient we had already defined something known as the Location Services CallBacks.
So we implement the required interfaces and define the respective methods. So for example, the onConnected() gets called when the request to connect the client finishes successfully.
Now so far everything sounds good. The problem arises when I try to connect the above part with the below mentioned :-
Now before even we had defined the Location Services CallBacks, we had defined an inner class inside the MainActivity class called, ErrorDialogFragment. This class is I guess used to create an error fragment (like an alert box) that can display error messages to the user in case apparently the connection attempt to the Location Services fails due to some reason. So there are some things that I do not understand here :-
What is the use of CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; The documentation in the page says :- Define a request code to send to Google Play services. This code is returned in Activity.onActivityResult. I do not understand this.
When is the onCreateDialog() inside the ErrorDialogFragment class get called. I do not find an explicit call to this method anywhere in the sample code mentioned on the page in the URL mentioned above.
What is the connection between the onConnectionFailed() and the error fragment.
There are 2 more methods defined :- onActivityResult() and servicesConnected(). I understand to some extent the use of servicesConnected() - it is used to see if the GooglePlay services is available. Is it a user defined method ? And is it not ding the same things that were being done inside the callback methods onConnected(), onDisConnected() and onConnectionFailed(). If not how are they different from servicesConnected() ?
And finally I just do not understand what is the use of onActivityResult(), what exactly are we trying to do here ?
Please pardon my ignorance. I am completely new to Android Programming and am trying to learn concepts clear and sound. Please correct me wherever I went wrong or misunderstood things. I tried looking through Vogella resources but could not find much help. Any good resources explaining details that would make my above concepts clear would be great help.
Q : What is the use of CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; The documentation in > the page says :- Define a request code to send to Google Play services. This code is > returned in Activity.onActivityResult. I do not understand this.
A : CONNECTION_FAILURE_RESOLUTION_REQUEST is the request code you have defined, If MainActivity exits, onActivityResult gets the request code, one started with. i.e . CONNECTION_FAILURE_RESOLUTION_REQUEST (or USER_DEFINED_REQUEST_CODE)
This is specified by startActivityForResult(Intent,). This here is done through
connectionResult.startResolutionForResult(this,CONNECTION_FAILURE_RESOLUTION_REQUEST);
That itself calls startActivityForresult internally.
The usage is specified in the given sample code 'MainActivity.java' in the form of a comment before defining onActivityResult. This is quoted here as follows:
/*
* Handle results returned to this Activity by other Activities started with
* startActivityForResult(). In particular, the method onConnectionFailed() in
* LocationUpdateRemover and LocationUpdateRequester may call startResolutionForResult() to
* start an Activity that handles Google Play services problems. The result of this
* call returns here, to onActivityResult.
*/
Q : When is the onCreateDialog() inside the ErrorDialogFragment class get called. I do not > find an explicit call to this method anywhere in the sample code mentioned on the page in > the URL mentioned above.
A: It is called from here :
// Show the error dialog in the DialogFragment
errorFragment.show(getSupportFragmentManager(),
"Location Updates");
onCreateDialog adds the dialog to the dialog cache, and the show method calls it.
Q: What is the connection between the onConnectionFailed() and the error fragment.
A: onConnectionFailed is a callback method that is called when there is an error connecting the client to the google play service. The errors list can be found at
http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html in the 'Summary' section.
Now if the error occurred and it has some resolution, the error fragment will try to resolve it . Look at
http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#hasResolution()
AND http://developer.android.com/reference/com/google/android/gms/common/ConnectionResult.html#startResolutionForResult(android.app.Activity, int)
Here ‘this’ refers to the MainActivity and CONNECTION_FAILURE_RESOLUTION_REQUEST is the user defined request code called by startActivityForResult which is here implicitly called by
connectionResult.startResolutionForResult(
this,
CONNECTION_FAILURE_RESOLUTION_REQUEST);.
Q: There are 2 more methods defined :- onActivityResult() and
servicesConnected(). I understand to some extent the use of
servicesConnected() - it is used to see if the GooglePlay services is
available. Is it a user defined method ? And is it not ding the same
things that were being done inside the callback methods onConnected(),
onDisConnected() and onConnectionFailed(). If not how are they
different from servicesConnected() ?
A: Yes servicesConnected is a user defined method.
I am afraid it is not the same, it is checking if google play services is available or not through isGooglePlayServicesAvailable().
OnConnected() on the other hand will be called after isGooglePlayServicesAvailable returns true. So they act after the service is connected or disconnected , servicesConnected() just checks if it is connected or not.
Q: And finally I just do not understand what is the use of
onActivityResult(), what exactly are we trying to do here ?
This i hope is clear from the previous answers :)
In my application i want to let users sign in with their google account so i am using google play services. This works - they can login and logout. I also want to find out if the user is signed in the onCreate method when i pass from one activity to another. I am trying to use Google play services isSignedIn function for this but on onCreate method it always return false (because google play service trigger on onStart).
Why do I want it ?
Because, i have two login option. One is google account another is my own login panel. I want to know that is user sign in wtih google or my system or not on onCreate. So i will give direction my flow.
How can i achive this or do you have an alternative suggestion ?
Thank you for answers.
This is due to the Activity lifecycle. Every time an Activity goes into the background, it will get onStop. The Play Games API requires you to disconnect when this happens. When the Activity comes back into the foreground, it gets onStart, and then GameHelper will attempt to reestablish the connection. Until this happens, isSignedIn will return false. When the connection is restored (which should be very fast if the user had already signed in), the onSignInSucceeded callback will be called and from that point on isSignedIn will return true.
It's impossible to know, on onCreate, whether or not the sign-in will succeed. Instead, you should wait for either onSignInSucceeded or onSignInFailed. It is guaranteed that one of those two callbacks will be called shortly after the Activity comes into the foreground.
At that point, you can decide what to do, because it will be clear whether or not the user is signed in with Google+.
I am not certain how your app works but it sounds as though you may be calling GameHelper.onStop at the end of one activity when in fact you want the game to continue in another activity. I don't think you can do this (see the accepted answer here):
Google Play Game Services multiplayer with Activity switching
My app is arranged into fragments and I have one fragment which interacts with GameHelper. Therefore, once I have performed GameHelper.setup and GameHelper.onStart, I am able to call isSignedIn from any method in any fragment and get the correct response.
An alternative approach (which I have used in the past) is to save the result of isSignedIn in a boolean in shared preferences. You can then check this to determine whether the user is signed in or not.
I’ve written a library for Android, upon initialising the library I register for call backs for location updates, subscription status changes, library status changes and various other updates...
Originally, I wanted all these updates to call back to a controller in my android UI app, and then for the controller to launch activity A, pass on the messages from the controller to activity A and so on. Or launch activity B for signing up for a subscription and forward messages to this etc
However, it appears that there isn’t a way to achieve this - Because each activity is in isolation? Unless I’m mistaken?
So what are my options here? It sounds like I either have to use one activity for the whole app and swap the UI’s which after looking into it doesn’t appear to be the way to go?
I did try to subclass Application, which worked and gave me access to my library from an activity – but I want it the other way around. Is this possible? would wrapping the library in a service achieve what I want to do?
You could use broadcasts and send them from your library. Each of your activities would have to register a BroadcastReceiver in their onResume() method and unregister it in their onPause() method. This would be comparatively easy when you use a common base class for your activities. You could then send commands from your library to whatever activity is currently active.