Android KeyguardManager not returning any result - android

I am trying to implement `KeyManager' into my app. When authorized action is needed, user is prompted with pattern for unlock. I observe result of this in my activity and based on result I proceed to further action. Following is code I am using,
private const val RESULT_OK = 99
val km = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
val i = km.createConfirmDeviceCredentialIntent("Name", "Something")
i?.let { ind ->
startActivityForResult(ind, RESULT_OK)
// startActivityForResult(Intent(this#LoginActivity, AnotherActivity::class.java), RESULT_OK) //This works
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.i("TAG======", requestCode.toString() + "--" + resultCode.toString())
}
Whenever I am using startActivityForResult(ind, RESULT_OK) onActivityResult is not called but when I used any other activity like startActivityForResult(Intent(this#LoginActivity, AnotherActivity::class.java), RESULT_OK) , onActivityResult is getting called after activity is finished. Am I missing something? I am testing this on physical device (OnePlus 5t) running Android 8.1.

I found the answer after 2 hours of debugging ! This was happening because I was using RESULT_OK constant as private. As soon as I made it public, my KeyManager was working properly! I assume system was taking it as a 0 and hence it was not returning anything to activity :)

Related

Network International Payment Sdk for NGenious showing error

I am working on NGenious payment SDK and trying to implement it in my Kotlin project but when the SDK shows up in Fragment, after 2 to 3 seconds the screen automatically got white and nothing is being shown.
This is the payment SDK
Method to launch payment Fragment
activity?.let {
PaymentClient(it).launchCardPayment(
CardPaymentRequest.builder()
.gatewayUrl(paymentOption.paymentCreateOrderResponse.data?.links?.paymentAuthorization?.href?:"")
.code(Constants.currencyCode)
.build(), CARD_PAYMENT_REQUEST_CODE)
}
onActivityResult Method
override fun onActivityResult(
requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.e("onActivityResult","onActivityResult" + resultCode)
if (requestCode == CARD_PAYMENT_REQUEST_CODE) {
when (resultCode) {
Activity.RESULT_OK -> onCardPaymentResponse(CardPaymentData.getFromIntent(data!!))
Activity.RESULT_CANCELED -> onCardPaymentCancelled()
}
Log.e("resultCode CCPF","" + resultCode)
}
Log.e("CPRC CCPF","" + CARD_PAYMENT_REQUEST_CODE)
}
As I review your CardPaymentRequest builder you are sending the Currency Code in the code field thats why they throwing an exception on it.
pass payment code in the code field, you can get the code by this field.
paymentOption.paymentCreateOrderResponse.data?.links?.payment?.href?.split("=")!!.get(1)
Now run the code like that.
activity?.let {
PaymentClient(it).launchCardPayment(request = CardPaymentRequest.builder()
.gatewayUrl(paymentOption.paymentCreateOrderResponse.data?.links?.paymentAuthorization?.href?:"")
.code(paymentOption.paymentCreateOrderResponse.data?.links?.payment?.href?.split("=")!!.get(1))
.build(), requestCode = CARD_PAYMENT_REQUEST_CODE)
}

How to add onCompleteListener when signing in with Firebase Auth pre-built UI?

I am using prebuilt Firebase UI to authenticate user such as below:
val providers = arrayListOf(
AuthUI.IdpConfig.EmailBuilder().build(),
AuthUI.IdpConfig.GoogleBuilder().build())
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN)
How do I attach a OnCompleteListener during sign-in? More specifically, I want to call getAdditionalUserInfo().isNewUser() to check if the user is first time log in. I know I can add onCompleteListener if I sign in using email and password-based method, but I want to handle multiple sign-in providers using the prebuilt UI method above.
In onActivityResult you receive the result for the sign in with some data passed as Parcelable which u are already overriding for validation!
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == RC_SIGN_IN) {
// this class has all the needed data !
val response = IdpResponse.fromResultIntent(data)
if (resultCode == Activity.RESULT_OK) {
// Successfully signed in
val isNewUser = response?.isNewUser!! // flag to check if the user is first time login
}
}
}
#See class IdpResponse of package com.firebase.ui.auth:-
Dug a bit deep for you ;) Happy coding!

Android seems to be ignoring my setResult call for onActivityResult

I have 2 apps, one a very simple toy app that exists to call the other:
const val AUTHENTICATE_CODE = 42
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener {
Intent(Intent.ACTION_VIEW, Uri.parse("testapp://hello.world/")) //2nd app has intent filter to intercept this.
.also { intent -> startActivityForResult(intent, AUTHENTICATE_CODE) }
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val textView = findViewById<TextView>(R.id.hello_text)
if (requestCode == AUTHENTICATE_CODE && resultCode == Activity.RESULT_OK) {
requireNotNull(data) {
textView.text = "Error: Intent was null. Data lost."
return#onActivityResult
}
val dataExtra = data.getStringExtra("com.example.app.DATA")
requireNotNull(dataExtra){
textView.text = "Error: Intent did not contain data."
return#onActivityResult
}
Log.d("TestAppPlsIgnore", "Result Intent received")
textView.text = "Success! $dataExtra"
} else {
textView.text = "Something went wrong. Request = $requestCode; Result = $resultCode"
}
}
//...
}
The other app is a little more involved:
The activity in app 2 that the toy app launches implements the navigation library from Jetpack.
Most of the fragments that are in that activity's nav graph implement the same ViewModel. i.e. private val mainViewModel by activityViewModels<MainActivityViewModel>()
Inside the MainActivityViewModel is a LiveData<String> that we'll call data. The MainActivity of app 2 has an observer watching data similar to this:
val dataObserver = Observer<String> { data ->
val result = Intent()
result.putExtra("com.example.app.DATA", data)
Log.d("MainActivity.DataObserver", "Sending data $data")
setResult(Activity.RESULT_OK, result)
finish()
}
mainViewModel.data.observe(this, dataObserver)
In the general flow to get to a point where a string is put into data, the navigation view of the main activity will likely navigate between one or more fragments.
The expected result: When a string is added to data in app 2, the observer will create the result intent, set it as the result, and finish app 2. App 1 will receive the result and call onActivityResult, and we should display "Success!" plus some data.
What I get: The observer does work. The log statement shows the correct data was received by the observer. App 2 finishes. And app 1's onActivityResult displays the fail case, showing the correct request code, but a response code == Activity.RESULT_CANCELLED. If the requireNotNull(data) statement is moved outside the if statement, app 1 will instead show that the intent returned was null.
My questions:
RESULT_CANCELLED is not being explicitly returned, and I am attempting to return an intent with data. So that should only leave the activity crashing as a reason why RESULT_CANCELLED is being returned. Navigating across a nav graph will inevitably cause some fragments to reach the end of their lifecycle. Would Android confuse that for an activity crashing?
Why is there a null intent when onActivityResult is being called? For the most part, I'm just following what's outlined in the documentation, if a bit more verbosely.
Is this not the right way to send a simple string between two specific apps? I don't want to use share intents, because this is meant to be a more direct communication between specific apps rather than a broad communication between my app and a category of apps.
It turns out that you should not call finishAfterTransition() elsewhere in an app if you plan to use an Observer-based setup like mine to send data through startActivityForResult(). finishAfterTransition() causes a conflict with any calls to finish(), and you'll send a null result and a ResultCode of RESULT_CANCELLED.

Getting error: override method should call super.onActivityResult

I am trying to capture an image from the camera and display it on the imageView. I tried but getting the error "override method should call super.onActivityResult", you can see my code below. Please let me know if I am doing it right.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
try{
val file= File(currentPath)
val uri = Uri.fromFile(file)
val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setImageURI(uri)
}catch(e:IOException){
e.printStackTrace()
}
}
if (requestCode == PICK_PICTURE && resultCode == Activity.RESULT_OK) {
try{
val uri = data!!.data
val imageView = findViewById<ImageView>(R.id.imageView)
imageView.setImageURI(uri)
}catch(e:IOException){
e.printStackTrace()
}
}
}
add super.onActivityResult(requestCode, resultCode, data); as first line on the onActivityResult method.
What does it mean?
When a class extends another, say you have class A and class B : A
In this context, class A is the super class of B.
So if class A has a method called:
fun someMethod()
you can do:
var myB = B()
myB.someMethod()
this effectively calls the code in A, because B extends A, and someMethod is not private.
Now if you want to modify someMethod's behavior in B, you override it...
override fun someMethod() {
// b does something different here
}
now, you can mark A's someMethod as #CallSuper (an annotation coming from here.
In which case you get the warning/error that B must call its super (know as Parent too) class too...
so B must now do:
override fun someMethod() {
super.someMethod()
// your B code too..
}
There's no rule to call it at the beginning you can call it at any time, as long as you do before the end of the function. In some instances, it's desired to call it as the first thing (if, for instance, A does something you need in B as well), and in some other cases, you want to wait for B to do something before calling super.... As long as you do.
onActivityResult is marked as such, and therefore you must call super.

onActivityResult showing error when back from the Calendar

I am using the following code to open the calendar app:
class Appointments : AppCompatActivity() {
lateinit var tv:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_appointments)
tv = findViewById(R.id.textView4)
tv.setOnClickListener(View.OnClickListener {
var callIntent = Intent(Intent.ACTION_EDIT)
.setType("vnd.android.cursor.item/event")
startActivityForResult(callIntent, 3);
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == 3 && resultCode == Activity.RESULT_OK && data!=null){
Toast.makeText(this#Appointments,"Some data came",Toast.LENGTH_SHORT).show()
} else{
Toast.makeText(this#Appointments,"Some Error",Toast.LENGTH_SHORT).show()
}
}
}
I keep getting 'some error' message. I tried removing 'data!=null' but I think the resultcode is the problem.
What I 'finally' want to achieve is this:
User opens the app
User clicks on a button to open the calendar app
User is able to see the calendar and then the user makes an appointment in the calendar
User comes back to the app and I am able to extract the date and time of the new appointment
Is it possible to do? If yes then some code example will be much appreciated.
If it is not possible then what are the other ways to achieve this?
Remove the call to super. Calling super will change the requestCode. This fact isn't exactly clear in a lot of documentation but I have spun my tires on it in the past. Similarly, you will find this answer Wrong requestCode in onActivityResult useful if you encounter a similar issue with fragment to activity communication

Categories

Resources