Wait for result from another activity - android

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.

Related

android asynctask result in another activity

I have an Activity A and an AsyncTask, which does some computations and stores data in to the database. These operations takes around 3-5 seconds. An AsyncTask is called after pressing a button "Save" and new Activity B starts (this Activity B is not important). There is also Activity C, that loads data, which I stored from Activity A and AsyncTask. And here is my problem - if I start Activity C and AsyncTask still didn't finish storing data, I want to show loading animation until AsyncTask will finish and data can be loaded in activity.
I have a vision that AsyncTask in method onPostExecute will change global variable "boolean finished = true" and after start Activity C, I will periodically check, if variable is true. But I think, that it's not a right way.
So, what is the right way?
Thank you
What you can do in Activity C is check the database in onResume whether your expected result is already there or not. If it's there, simply show your result. If it's not there show your loading icon and register a broadcast receiver that listens for a specific event.
In onPostExecute of the AsyncTask you can than broadcast that specific event. When activity C receives that event it can simply recheck the database where the result will now be.

Bring windows application ShowDialog mechanism to android

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.

Android does startActivity() calls get stacked?

I have recently started working with android. So I am in the initial stages of learning.
The question is: when we call startActivty(Intent) in the middle of execution of another activity.I actually thought that startActivity() will simply jump into the activity called. Isn't it the case? does it simply stacks the call to the activity?
I am getting this doubt bcoz..I actually have a program in which Activity A has a loop. Some where in the middle of loop Activity B is called. I want activity A to resume with the loop only when activity B finishes. I have made a call in A's loop like this:
in = new Intent(this,MyChoiceActivity.class);
in.putExtra("McObj", mc);
startActivity(in);
finish();
So What happens is Activity A calls B, but B is not entered, A simply resumes the loop and again calls B, simply the calls to B are stacked and once the loop in A completes, one by one calls to B in stack are executed so finally the first call to B is executed last(last in first out)...But I dont want the order to change...If A calls B, it should simply go and execute B and only then come to the loop..what should I do to accomplish this?
Your activity can be busy and just 'be there'. If you start a new activity while your activity is really doing something, it will not 'wait' until the other activity is finished: both can be around at the same time.
If it would do this, it would have you end up with a lot of busy-waiting processes, so luckily this is not what happens.
See the activity lifecycle to find out more about what an activity is (it is not just another class as you are using it now, it starts a separate thing that hangs around next to (not instead of / on top of) your current activity).
Instead, beacuse these things go rather asynchronous, you might want to use a different approach: use an ActivityForResult, and start a new one when you get that result.

How to finish an activity and return to a varied location?

My application implements an action which stretch upon 3-4 activities.
the entry point to this process can be from different activities in the application. (HomeActivity, various displayActivities). I want to return to the starting activity once the last activity is finished successfully.
Is there a best practice way to do it? thankyou.
You can use a global static boolean to help you with this (in the example SomeClass.IsClosingFlow),
Plus, you should define each activity to "mark" if it's in the "flow" (flow=meaning it's part of the pack of activities that need to be closed). I recommend using this mark as an abstract method if you all your activities are extending some abstract-activity (i.e. isActivityInFlow()).
The following code demonstrates this,
It needs to be places in onResume() of each activity in the application:
// Check to see if we are in the process of closing activities
if (SomeClass.IsClosingFlow){
if (isActivityInFlow()){
this.finish();
}
else{
// If we got here, and we're not in the flow anymore
SomeClass.IsClosingFlow = false;
}
}
There's a bunch of different ways to manage this kind of application flow. One way that may work for you and is pretty easy is to just use startActivityForResult() when you call from one activity to the next. Then, when you are finally done, you just set the result and call finish(). In each activity you can then implement onActivityResult() so that it sets the result and calls finish() on itself. In this way you can chain forward a number of activities and then chain back to whoever started the chain.

Which variables are involved in the return of Activity results?

I'm seeing inconsistent behaviour when calling setResult() and finish() on some activities that were started using startActivityForResult(). From other questions on here, it looks like a variety of reasons for unexpected behaviour are possible. Which variables do I need to look at when debugging for a complete picture of what is happening during the process of starting an activity for result and sending it back, given that the Activity may start Activities for result itself?
Specifically, I'd like to know how to see:
How the result will be treated when it gets back to the starting activity
Where (and if) a result will be sent when finish() is called
What the currently set result is
I'm already watching:
mResultCode
mResultData
mParent
But they don't give enough information their own. I'd like the entire state of my application's result mechanism.
request code ...
when you call startActivityForResult(), you supply a request code. the request code allows you to match up a specific activity result with the particular start request. specifically, in onActivityResult(), you will be passed a request code that allows you to make that comparison.
result code ...
the result code is just a way of doing coarse-grained messaging back from the started activity to the starting activity. that is, the started activity can set a result code by calling setResult(int) which is returned to the starting activity in onActivityResult(). again, this is typically used to perform coarse grained messaging like SUCCESS or FAIL.
result data ...
finally, to pass finer grained data back, use the variant of setResult(int, Intent) that takes both the result code and an intent. when you construct the intent, add whatever extras you wish to pass back to the starting activity.

Categories

Resources