Prepare an activity before show - android

The problem is only when my activity already shown it begins updates and it look no good. How it works: in onStart of activity I send cmd to service to get update data, also I register brodcast listener there. I want to prepare received data from service to show before activity appears. How to do that? Thanks.
How it works now: when I back from another activity first I see old data and then it changes (very fast but you can see it) to new.

if you want to setup things before your activity is shown you need to do things in the onCreate method instead of onStart method.
further informations in android documentation

When you send the command to the service to update your data, you should change the views in your Activity to show loading indicators, or a blank view, or whatever you want the user to see until the Broadcast comes in that your new data is ready. Then you shouldn't need to worry about old data being visible.

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?

Prevent screen display until after fetching data

Is there any way I can go from one activity to another and delay the display of the screen on the target activity?
What I want to be able to do is to allow the target activity to fetch its required data but not to display anything until does.
I want the screen of the source activity to still be visible until I am ready with the data in the second activity.
Specifically, I am using an AsyncTask to fetch the data.
I know I could fetch the data in the source activity and then send it on to the target activity but this is not viable in our case.
Edit: More Info:
The whole reason I want this is because I am trying to change the structure of certain parts of the current code.
At present the way it works is the the first activity gets the data and then sends it to the second activity as a bundle.
This created problems when the second activity could be invoked from multiple places. It resulted in loads of duplicate code.
So, I decided to move the fetching of the data into the target activity thus cutting out any need for repeating code.
it also makes more sense for the activity to fetch its own data rather than relying on something else sending it.
You should first make a service that runs your async task. Then, start the service from your first activity with startService(new Intent(this, UpdaterServiceManager.class));
When the task ends in the service, start the second activity.
Click here for an excellent service tutorial.
Try to use service for this purpose.

pass data to background activity without change the current activity

I have 2 activity.
Activiy A will list links to download.
Activity B has a listview of download item.
When I click the link in activity A, how to send the link to activity B to download without change activity A (while activity B still downloading on background) ?
You need to understand that Activities Dont need any result to work, activities need data. And Activities can use these data to load contents of its. And there is no use of this data into activity untill its into background. SO Here is solution:
Create some Data ArrayList or Flag, Global to the application.
Changes this Data into Second activity, which is in forground, and want to notify about some result.
When First Activity which is in background, and want to listen results, check for changes into data in onResume method, and on change load new contents.
My approach would be:
Activity A: Shows a list of items to download.
Service B: Downloads the item in the background. Maybe you can queue
multiple download items.
File C: When you finish downloading something you should save that
fact in a file.
Activity D: When this activity starts it should read 'File C' and
display its contents.
You can't really send data to an Activity without starting it. When an Activity is not visible it is pretty much asleep.
You should read this: Application Fundamentals, read it many many times ... then read it again. I still go back and read it after years of working with Android.

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.

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