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.
Related
I'm working on a library that has a couple of ready-made activities.
So far i have my activities in the library, and in the main app, i call it normally with registerForActivityResult to start it.
this means whoever is using my library would be able to see the whole activity.
what i would like to do, is to have the developer call a method in the library class and ask it to do an action, and in the library that method would on its own start the activity, register it for result, and return the result to the calling class through an interface.
the below is what i tried but it gives me error LifecycleOwner is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED
private fun launchScannerActivity(activity: FragmentActivity, callback: ScannerCallback) {
val scanResult =
activity.registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
if (it.resultCode == Activity.RESULT_OK) {
callback.onResult(it.data?.getStringExtra("Some Key") ?: "")
} else {
callback.onFail()
}
}
val intent = Intent(activity, ScannerActivity::class.java)
scanResult.launch(intent);
}
why do i need this:
This library would be an SDK for a SAAS product, so we would like to abstract and obfuscate as much of the implementation as possible from our clients.
You can't really communicate between Activities using interfaces, at least not in a way that is somewhat concise and isn't very prone to leaking. What you can do is expose your own Activity result contract. Then your API could be as simple as some of the ones in ActivityResultContracts. You can look at the source code there to see how to implement it.
Maybe something like this:
class ScannerResultContract : ActivityResultContract<Unit, String?>() {
override fun createIntent(context: Context, input: Unit?): Intent {
return Intent(context, ScannerActivity::class.java)
}
override fun parseResult(resultCode: Int, intent: Intent?): String? {
return if (resultCode == Activity.RESULT_OK) {
intent?.getStringExtra("Some Key")
} else {
null
}
}
}
Client usage:
// In activity or fragment:
val getScannerResult = registerForActivityResult(ScannerResultContract()) { resultString ->
if (resultString != null) {
// use it
} else {
// log no result returned
}
}
//elsewhere:
someListener.setOnClickListener {
getScannerResult.launch()
}
startActivityForResult(intent: Intent!, options: Bundle?) has been deprecated. I am trying to replace with ActivityResultLauncher but I need to pass the options. How can I do this with the new method? Below is an example of the original (now deprecated) method that would open the Contacts menu and then do one of two things in the switch based on the value of code:
...
val code = contactType //can be either 1 or 2
val contactsIntent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
contactsIntent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
startActivityForResult(contactsIntent, code)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(resultCode == Activity.RESULT_OK) {
when(requestCode) {
1 -> { //Do something
}
2 -> { //Do something else
}
}
}
}
I have tried to convert the above to use ActivityResultLauncher but I haven't figured out how to pass the value of code to it. Below is what I have so far:
val code = contactType //can be either 1 or 2
val contactsIntent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
contactsIntent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
contactLauncher.launch(contactsIntent) //or maybe contactLauncher.launch(contactsIntent, code)?
private val contactLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if(it.resultCode == Activity.RESULT_OK) {
when(??? requestCode ???) {
1 -> { //Do something
}
2 -> { //Do something else
}
}
}
}
In this situation, you'll need to create two separate ActivityResultLauncher objects, one for each case.
IMO, this is exactly what Google was trying to solve, having a cluttered "onActivityResult" function, as well as having to deal with requestCodes. Right now, this is more similar to an OnClickListener kind of callback.
They did the same with other parts of Android, like requesting app permissions. The requestCode is now handled internally.
An overloaded version of 'launch()' allows you to pass an 'ActivityOptionsCompat' in addition to the input.
#see:
https://developer.android.com/reference/androidx/activity/result/ActivityResultLauncher
In Android/Kotlin, I am launching a new activity with startActivityForResult in my onCreate function, and get the returned variable (let's called it X) from onActivityResult outside of the onCreate function. Subsequently i want to access the variable X in the onCreate function to display it on the screen. However it never displays the data as if it were empty.
Any ideas of what I might be doing wrong? Thanks
Here is the code. When the user clicks on notesView it launches a new activity (Notes2Activity) where the user can enter its note in a full screen. Then upon validation of the note the code return to the previous activity where I am trying to edit the content of NotesView to returnNotes, but the app crashes.
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new_contact)
val notesView: TextView
notesView = findViewById<TextView>(R.id.inputNotes)
notesView.setOnClickListener {
val intent = Intent(this, Notes2Activity::class.java)
startActivityForResult(intent,2)}}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {if (resultCode == Activity.RESULT_OK) {
if(data != null) {
val returnNotes: String = data.getSerializableExtra("notesEntered") as String
var returnNotesInputs = returnNotes
notesView.setText("")
notesView.append(returnNotes)
Log.d(TAG, "note returned to newContactActivity is: $returnNotes")}}
else if (resultCode == Activity.RESULT_CANCELED)
{Log.d(TAG, "user clicked canceled in Notes2Activity")}
}
You can save the data in a global class field(s) and call recreate() on the activity after that. This will essentially call onCreate() again on the activity and then you can use the data in the fields.
IMO, this is a bad way to handle the situation. The specific logic from onCreate() can be exported to a private function. Then you can call this private function from onActivityResult() as well as onCreate().
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.
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