As you know, we should use startactivityforresult and onactivityresult to request and return a value between activities in android. But it is hard to manage when you need to get results multiple times in one method (it is easy when you want to start activity at the end of code).
What I mean by 'windows application ShowDialog mechanism' is how to pause (or block) the method when startactivityforresult is called, and continue when the result is returned by the second activity.
Related
There seem to be (at least) two ways to send Intents in Android:
PendingIntent.send(...)
Activity.startIntentSenderForResult(PendingIntent.getIntentSender(), ...)
Other than the fact that the latter only works starting API level 5 and that the results are passed back in a different way (via PendingIntent.OnFinished vs. Activity.onActivityResult(...)) is there any fundamental difference between the two?
I find the first one a lot more convenient as it can be entirely encapsulated inside a library without requiring the calling activity to override onActivityResult(...) to forward the result (like this: yuck!). Is it ok to still use that approach?
A quick clarification, because I've seen someone complain about this on another question: The methods above are not static methods. I wrote them that way simply for readability.
Seems like these two approaches are very different:
The start...forResult(...) methods start an intent or sub-activity in a way that allows for a result to be returned to the activity that executed the start...forResult(...). The result will be passed back to the activity's onActivityResult(...) method.
All other ways of launching intents or sub-activities (including PendingIntent.send(...)) act in a fire-and-forget-manner and don't allow for any results to be returned. The OnFinished handler is called as soon as the launch is sent, whether or not it takes a while to complete. The data passed into this handler, therefore, does not necessarily have anything to do with what you would otherwise receive via onActivityResult(...). In fact, in my case, the OnFinished handler is always called right away, before the dialog of the sub-activity even shows up, with a resultCode of Activity.RESULT_CANCELED.
What a mess...
I have activity one that calls activity 2
When user close activitie 2 and gets back to activity 1 i want to do some proccess only when activity 2 onDestroy is finished
I thouht to pass some interface to do a callback from the end of onDestroy
Not sure what is the best way to do it
Any suggestions?
You can use a custom intent with a broadcast receiver
:Good Example
The framework doesn't allow having 2 activities in your app running concurrently, so I don't think your idea with an interface sould work.
A simpler alternative that might fit your requirements is to use StartActivityForResult in Activity 1 to launch A2. A1 will then get the onActivityResult callback when A2 finishes (for whatever reason)
Look at the API for details.
Here is the scenario. When the screen goes blank or if returning to an activity from outside the application (phone call, multitasking) I want to be able to call a function in the onResume() function.
I don't want this function called if I used the back button from within the same Application. What I am doing is requiring the person to supply sensitive credentials again if they return to the app from outside sources.
I feel like the answer may lie with intent filters but I have not found anything doing exactly what I need.
you can start your activities with startActivityForResults and override onBackPressed for other activies to return a specific result.
catching the results you'll know if a back button was pressed.
In an activity (say A) I have to perform a certain task depending on the result of another activity (B). I start activity B using startActivityForResult(). The pseudo code is something like this:
(in activity A)
//Statements
//startActivityForResult(activityB)
//get the result in some local variable for activity A. result is a boolean
//if(result==true) do something
//else do something else
Now the problem I'm facing is that after starting the activity B, it doesn't wait for the result to arrive from B. Instead, it proceeds and uses the default value of the boolean result.
Any solution?
If this particular block was in another thread, i could write a synchronized block and
issue a wait() after starting the activity B, and then notify() in the onActivityResult().
But since there's just a single thread, that's not an option right?
Should mention that activityB takes a user input of Yes/No and returns that. So on starting it, the result is not immediately available
Any solution?
Put your last two lines from your code listing in onActivityResult(), as is described in the Android documentation.
More importantly, you also need to rewrite activityB to actually follow the instructions for using setResult() to pass results back to activityA.
Can anyone help me to understand synchronous and asynchronous activities in Android?
What is exactly meant by synchronous and asynchronous activity in Android?
StartActivity, StartSubActivity and StartAcivityForResult start an activity synchronously or asynchronously, or can they behave in both ways?
Please explain as I have gone through many articles but could not find any proper explaination over this.
First of all, only one activity can be running at a time on Android, so you'll never have two activities running at the same time. Use startActivity() when you want to "fire and forget", that is, you want to launch an activity but are not expecting it to return a value to your activity. In that case, the new activity will start and your activity will be paused; you might eventually regain control once the user returns to your activity.
Use startActivityForResult() when you are expecing a result from the activity you are launching. In this case, the calling activity should override onActivityResult(), which will be called when the launched activity exits and has a result to return to you (which it sets with setResult()).
In both cases, since the calling activity and the called activity are in the same task, it's "synchronous" in a certain sense (although I think using the terms "synchronous" and "asynchronous" can be confusing in this context). The calling activity won't appear on the screen until the called activity finishes.
A useful read to know more is:
* http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html
-
Bruno Oliveira (Android Developer Relations, Google)