I am using FirebaseUI Auth to authenticate users with gmail. I am signing to with my gmail. In onActivityResult I check result code and it is RESULT_OK. Although when I create IdpResponse from intent, I get null and I can't check whether it's a new user or not.
I have added google-services.json in the application, also added libraries and plugins in gradle.
here is my code :
fun promptUserSignIn() {
val providers = arrayListOf(AuthUI.IdpConfig.GoogleBuilder().build())
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
providers
).build(), SIGN_IN_REQUEST_CODE
)
}
#Override
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_REQUEST_CODE) {
val response = IdpResponse.fromResultIntent(intent)
if (resultCode == Activity.RESULT_OK) {
Log.e(TAG, "response: $response intent: $intent")
}
}
}
Related
How to detect when user didn't allow for open bluetooth?
if(!isBluetoothOpen()) {
val enableBluetoothIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
if (isPermissionGranted()) {
startActivityForResult(
enableBluetoothIntent,
REQUEST_ENABLE_BLUETOOTH
)
}
As per documentation https://developer.android.com/reference/android/bluetooth/BluetoothAdapter
Notification of the result of this activity is posted using the Activity.onActivityResult(int, int, Intent) callback. The resultCode will be Activity.RESULT_OK if Bluetooth has been turned on or Activity.RESULT_CANCELED if the user has rejected the request or an error has occurred.
Code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
when (resultCode) {
Activity.RESULT_OK -> { // User approved permission }
Activity.RESULT_CANCELED -> { // User rejected permission }
}
}
}
I want create a file to specify folder ,my file type is zip,I want use
FileOutStream to create.
It sends an intent, you can see the code below:
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(mContext,intent, 9999,null)
then it gets the returned data, you can see the code below:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {
9999 -> {
Log.v("Test", "Result URI " + data!!.getData())
// FileInputStream(data!!.getData())
}
}
}
}
}
the log is
V/Test: Result URI content://com.android.externalstorage.documents/tree/primary%3ATestFile
I want to create file with the Uri,but use FileOutStream will crash,have any way to create file in specify folder?
I am trying to implement the Firebase AuthUI for a dummy app following the course by google itself on udacity. I am not getting the same output as in the course itself and idk what to do exactly or what is going wrong. Below is the code i have written to start up the AuthUI:
private fun launchSignInFlow() {
val providers =
arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build()
)
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
providers
).build(), SIGN_IN_RESULT_CODE
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SIGN_IN_RESULT_CODE) {
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
Log.i(
TAG,
"Successfully signed in user " +
"${FirebaseAuth.getInstance().currentUser?.displayName}!"
)
} else {
Log.i(TAG, "Sign in unsuccessful ${response?.error?.errorCode}")
}
}
}
this is the output screen/ui that i get from this code
And this is what the output should be like according to the course i am following
What's worse is that even when i try to enter an email to the output i get, the screen gets stuck and does nothing at all. This is the link for the video of google for this specific task
https://youtu.be/JKZpZvbm3Bk
Although everything work as intended and I get results from the Places API, the onActivityResult callback is never called when the user selects a result. What happens is that the result appears in the search textview and the Autocomplete activity doesn't exit. I have to press back for it to close.
// Set the fields to specify which types of place data to return.
val fields = Arrays.asList(Place.Field.ID, Place.Field.NAME)
// Start the autocomplete intent.
val intent: Intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields
).setTypeFilter(TypeFilter.ADDRESS).
setCountry("DE")
.build(requireContext())
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(data!=null) {
var status: Status = getStatusFromIntent(data)
//var place: Place = getPlaceFromIntent(data)
}
super.onActivityResult(requestCode, resultCode, data)
}
Do you run method startActivityForResult inside Fragment? Check this out: onActivityResult is not being called in Fragment
You have to override onActivityResult in Activity which contains your fragment.
// In your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
This will call your fragment's onActivityResult()
Ensure you're using the latest version of Place client SDK Places 2.1.0
. I don't see any reason in your code that causes the behavior you've addressed. Just use the below implementation and try.
private fun pickNewPlace() {
if (!Places.isInitialized()) {
Places.initialize(context!!, getString(R.string.google_service_api_key))
}
// Set the fields to specify which types of place data to return.
val fields = listOf(Place.Field.ID, Place.Field.NAME)
// Start the autocomplete intent.
val intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.OVERLAY, fields
).setTypeFilter(TypeFilter.ADDRESS).setCountry("DE").build(context!!)
startActivityForResult(intent, REQUEST_CODE_DEFAULT)
}
onActivityResult result handling
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_DEFAULT) {
if (resultCode == RESULT_OK) {
// Get relevent info from place object
val place = Autocomplete.getPlaceFromIntent(data!!)
} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {
val status = Autocomplete.getStatusFromIntent(data!!)
showAlertMessage(status.statusMessage ?: getString(R.string.something_went_wrong))
}
}
}
I am using following code to select picture from gallery. I want to track the int value from this intent.
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
intent.putExtra("Position", 1)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE)
But I am getting only default value[0] when trying to get the passed value from intent in onActivityResult.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
val selectedImageURI = data?.data
val position = data?.getIntExtra("Position", 0)
}
}
}
So my doubt is, is it possible to track the values through intent chooser? If yes, how?
you have to use requestCode in this line:
startActivityForResult(Intent.createChooser(intent, "Select Picture"), requestCode)
// for example use SELECT_PICTURE_POSITION_1 as requestCode
then you get it back here:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {// use your request code here
SELECT_PICTURE_POSITION_0 -> {
val selectedImageURI = data?.data
val position = data?.getIntExtra("Position", 0)
}
SELECT_PICTURE_POSITION_1 -> {
val selectedImageURI = data?.data
val position = data?.getIntExtra("Position", 1)
}
SELECT_PICTURE_POSITION_2 -> {
val selectedImageURI = data?.data
val position = data?.getIntExtra("Position", 2)
}
//other conditions here
}
}
}
use multiple request codes for your needs.
is it possible to track the values through intent chooser?
No. Your problem has nothing to do with the chooser. You would have the same results with startActivityForResult(intent), SELECT_PICTURE).
There is no requirement for the activity being started by startActivityForResult() to return to you any extras that you happen to have put in that Intent.