Prevent screen display until after fetching data - android

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.

Related

Prepare an activity before show

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.

Android How to transfer data between activities without moving to another activity

How can i transfer data between two activities, without actually move to that activity? The scenario is as follows:
Activity A is a splash screen. While the splash screen is showing, some data is being generated in the background of type ArrayList. After This data is loaded, i need to start Activity B (lets say a Login screen) and just before that i need to transfer the data to Activity C. I know i can use PutExtras for transferring data, but wont this run the target Activity instead only sending the data?
Thanks
The full flow of the app is as follows:
Activity A (splash screen) --> Activity B (Login screen) --> Activity D (some user interface and buttons) --> Activity C (the activity which should be able to load the data generated in the splash screen upon certain Button press).
I think You don't fully understand life cycle of Activity and application.
Rule of thumb, there is always just one activity, (it is the worst scenario when each activity has to be recreated when You go back to it) the one visible at the moment. You can't do things like start activity but don't show it yet or anything like this.
If You have expensive task to do like downloading data do it in AsyncTask
if this task has to be start and finish when You enter and leave Activity A use fragments
if it has to finish once You start whether Activity A is still existing or it was already destroyed use services
Places You can store this loaded data so it may be easily accessed later form any Activity is database with help of Loaders(loader is optional but really nice) or SharedPreferences.
If you want to pass data to an activity that is running and not start a new instance of that activity you just need to set flags on your intent. For example:
Intent i = new Intent()
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
will not re-launch an activity if it is already running at the top of the stack. If all you want to do is get data to Activity C which is running without starting it again you could try this or one of the other flags. There's a bunch of them. Just look look through the list and see if one fits your needs.

Update ListView of previous activity in background

I have question regarding my previous ListView activity.
I have my ListView which is continue updating using socket connection, now when I click on any of the list row i can go to my next screen now when i come back to my previous ListView screen my ListView is start updating again but i want to keep updating my ListView in a background when i am on my nextscreen.
Something like i want to keep my previous screen alive when i am on my nextscreen.
Sounds to me like your the code you are using to load the data for your ListView is tied to your Activity.
For instance you have an AsyncTask or Thread in your Activity that cointains your ListView and you use it to download data, or do whatever is needed to get the data to populate the list. I also assume you start it in one of the Activity lifecycle methods e.g. onCreate().
If that is the case then you should consider seperating the code used for getting the data for the list from your activity code.
One way to do this is to use a Service which will be able to run independantly of the rest of your application and do all the heavy lifting involed with fetching the data for your list. You can communicate with the service from anywhere in your application. The Service can save the data to a database. Then all you have to do in your Activity is query the database and populate the adapter to get the latest data without much delay.
You can read more about services here:
http://developer.android.com/reference/android/app/Service.html
You could (and probably should) do what Paul suggested or you could change to way you switch your screens by using Fragments or a ViewFlipper. This way you still run your AsyncTask or Thread while doing something else on a different page.

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.

Handling of server requests and device rotations

I read a lot about handling rotation in android applications, but I still have so many questions and need to much to understand.
Let me explain my problem or implementation, that I'm using now in my application.
If an activity will be opened, a get request will be sent to server. This request will be executed in a Thread (new Thread(...)) and if request was completed, activity's ui will be refreshed.
But what should I do, if the user rotate his device?
By default, the activity will be destroyed and request will be started again and start a new thread, but the thread of destroyed activity may be still running.
I guess, it's a quite wrong way, I have now.
But what is the best approach, to handle this?
Probably is the best way to forbid rotation, but what If I don't want that?!
May be it's the second part of my question:
I saw a video form Google IO. Mr. Dobjanschi suggested to use services and to store retrieved data in content provider. So, probably I can use a service for executing my requests. But should data be replaced every time the get request was completed?!
Well dont know exactly how its done, You can try saving the instance and retrieving the same when config changes with following methods:
I have read about them but haven't really implemented them yet. I hope it can give you some start.
#Override
public Object onRetainNonConfigurationInstance() {
return(myServerThread);
}
private void restoreServerFunctions() {
if (getLastNonConfigurationInstance()!=null) {
myServerThread=(Thread)getLastNonConfigurationInstance();
}
}
You can specify that the activity handles the rotation itself. This is done through adding:
android:configChanges="keyboardHidden|orientation"
in the tag of the activity inside your android manifest. You don't have to actually handle the rotation but this will tell android to not destroy your activity. The base activity class will handle all the rotating of the user interface for you and your thread will be executed correct.
A small side note: if you are doing only a small server task use AsyncTask to execute the call to the server in the background instead of creating a thread. This will minimze some of the programming effort you need to communicate the results from the thread to the activity and update your UI.
One easy way, though I've never tried it. Instead of refreshing the current UI, when the thread finishes, start a new Activity with the just downloaded content. So first, you start an Activity with a blank page (or just the page's frame), then you rotate the blank page as much as you like, then the downloading Thread spawns a new Activity, replacing the blank page Activity with the loaded content page using the current orientation.

Categories

Resources