android asynctask result in another activity - android

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.

Related

Can I update the UI of an activity which is in background?

The question is more conceptual than coding-related.
I have an app with Activity A and B.
I call an AsyncTask from Activity A, while the call is being made, I don't want to block the user showing the progressdialog, so my user can freely move around the application without getting bored of waiting.
Now, the query is AsyncTask or lets say a Service is being called from Activity A which is responsible for downloading some kind of data from the server. While the call is being made, the user has changed the Activity and Activity A has gone to background.
Now, while the user is freely moving around the application, he just wants to come back to Activity A to check the download status,(which is something lets say I set some data to my TextView).
Now the question is, once the download is over , while my Activity A is still in background, my UI should be updated while Activity is still in background. This way the user feels he gets the data before he switches to Activity A.
Is this possible, if so how?
Summarizing my question, can I update the UI of an Activity while it is still in background with the Asynctask or Service which the Activity invoked to fetch data from server.
One post suggested that ideally I need to update the **UI in onResume(). My question is, is this the only approach?
once the download is over , while my Activity A is still in background, my UI should be updated while Activity is still in background. This way the user feels he gets the data before he switches to Activity A.
It isn't possible. You see, the Activity has to pass through the onCreate() and onResume() callbacks for the UI to be changed.
Also, you should be using a bound Service to update the Activity when it returns to the foreground.
onResume() would be the best approach. You may save the changes in a SharedPreferenes or Pass the data using Intent and show the changes before the UI is visible.
Another approach would be running a service and checking if the activity is visible. If its visible immediately update the UI or wait until user visits the activity. To check if the activity is currently visible see here,
How to check if activity is in foreground or in visible background?

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.

Android - Reduce Code in OnCreate to avoid Black Screen

I have an activity.Let's call it as Activity A. Clicking on a button in this activity sends data to a server ( a POST method) and also launches Activity B.
Since there is some computation involved before sending data to the server from Activity A, I have implemented the task of sending data to server as a Async task.
In Activity B, in OnCreate method there is a GET request which gets fired. So as a result when Activity B launches, this GET request gets fired. After getting data from the server I load the data in the list view. All these are in the OnCreate method. Again, I have implemented this GET request as a async task.
Also, the POST method in Activity A and the GET method in Activity B are inter-related.
The issue I am facing is when I click the button in Activity A, the screen goes black and appears after 10 seconds (Activity B) . I have implemented a progress bar in Activity B and made it to appear till I get a response from the server. But this does not appear at all. All I am getting is suddenly the listview getting populated.
Wondering why this appears as the task is an async task in Activity A and B
Activity B
onCreate method
{
display progress bar
asynctask to fetch data
while(data not fetched){
// loop through until you get data
}
populate list view
}
Could any one please let me know where I am going wrong. This is the minimal code which need to be there in the oncreate method. Also, since this is a one time activity I do not want to implement this as a service. Also, I wish not to go with splash screen. All I want is the progress bar to get displayed till I get the data from the server. Kindly help to avoid this black screen when going from Activity A to Activity B.
Thanks
Could any one please let me know where I am going wrong.
The while(data not fetched){} bit is most likely the source of your difficulty.
Kicking off a task in onCreate() is fine. However, do not block processing of the rest of onCreate(). Delete the while loop, and have work in your task's onPostExecute() handle updating the UI when the data is ready.

doing work after onSaveInstanceState of Fragment

I have a Fragments which save user data in their onSaveInstanceState method.
I need to get this data when the user finishes the activity and display it in the next intent.
Currently the sequence of my implementation is:
user hits the close button
computation starts relying of data stored in prev calls of onSaveInstanceState, then start new intent
onSaveInstanceState of current fragments get called, information is missed in calculation
new Activity displays
old Activitys onDestroy is called
I thought about starting my calculation and the new activity in onDestroy. Then I need a mechanism to destroy the current Activity.
Or I could somehow call all remaining onSaveInstanceState method calls in the current thread before doing the calculation.
What would be better or is there a better way?
This sounds like a strange design to me for android. For this use case I would do:
user presses abitratry button (in your case close)
click listener gets the needed data (the same way you would get them in onSaveInstanceState)
start an asynctask if lengthly operation and show progress bar or make computation on UI thread if trivial
start intent with computation result for new activity
I have a Fragments which save user data in their onSaveInstanceState method. I need to get this data when the user finishes the activity and display it in the next intent.
Is there any reason you need the data specially from onSaveInstanceState() method?
thought about starting my calculation and the new activity in onDestroy. Then I need a mechanism to destroy the current Activity.
If you call something in onDestroy() there is no need to forcefully destroy the activity since its already in progress. (btw. finish() would do it). Appart from that, onDestroy should do clean up procedures e.g. free resources or close streams, not for calling new activities

Wait for result from another activity

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.

Categories

Resources