Android - Reduce Code in OnCreate to avoid Black Screen - android

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.

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.

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?

How to tell when an activity is shown

I have an app that contains a sequence of listviews, and those lists are populated with data retrieve from a webservice. I want to refresh this data when the user presses back to go to the previous list.
I have currently tried overriding the onWindowFocusChanged method, but this doesn't work, since when I start a webservice download, I bring up a progress dialog and close it upon completion. This causes a recursive effect (the dialog closes and gives focus back to the list activity).
Is there any way I can get when the activity is first shown? Similar to viewWillAppear for iOS?
Just override onResume() in the Activity. Just make sure to do whatever internet communication you do in a separate thread.

Event for when UI is shown to user?

I would like my main activity to load a loading message. Once this is displayed i would like to start my tab activity which has to load a fairly heavy RSS feed. Problem is i have tried overriding onStart and onWindowFocusChanged if hasFocus but neither draw the view before starting the other activity.
Anyone know how to do this? All the splash screen examples use a timer which is not what i want to do, i want it to stay while the data is loading and clear itself when the onPause is fired.
You cannot start another activity while you're still showing another one. You have to change you control flow.
Load you RSS feed with an AsyncTask while you're still in your main activity showing the loading message. When you finished loading all the data you need, store it somewhere, then start you tab activity. So you tab activity don't need to load the data itself but can fetch it from the place you stored it.

Android restart activity (with AsyncTask) on completion of another activity

I suppose the title is a bit confusing but here is what I'm trying to do:
I have a class called ManageClass which lists the entries of a database. I have written another private class within ManageClass which extends AsyncTask so that I can display a progress dialog while I'm getting the data from the database. Now when I click on an item I create a new Intent which takes me to my ViewItem class. I've added a button there so that the user can delete that particular entry that he/she is looking at. All of the above work fine.
Now I want after deleting that entry to kill the activity and go back to the previous one (the one displaying the list) but I want to refresh the listings.
My problem is that I cant use onResume() cause it will also be called when the activity is resumed after the AsyncTask finishes.
Could anyone help me with that? I'm really stuck... all ideas are welcome!!!
If I understand your app workflow you should use startActivityForResult instead of launching a new Activity via intent.
Look at here fore some example
Basically you can launch a new Activity and wait for a result via callback on the "opener" activity. so you can avoid to put your logic into onResume method

Categories

Resources