Start activity while pulling data from database - android

I have an application which stores data in SQLite database. Main activity is just a screen with a single button. When user clicks this buton the second activity opens. The second activity is a list (ListActivity) which should contain all the records from database. Here is the question: which approaches fits better: 1) when button is clicked, start AsyncTask, in doInBackground method pull data from database and send pulled data to the second activity as a parcelable array or 2) start the second activity, in onCreate method pull the data from database and display progress bar while selecting data. I believe it is not based on primary opinion and there should be valuable pros and cons. But I am new to Android and I'm struggling to identify better approach for this. Thanks for your attention.

First approach is not user friendly. Because when user press a button to open different activity(In terms of UI), then user except to show a transition, Not the stuck in first activity. Actually it depend on your application UX design. Also first approach has another problem. If you load data in first activity then you have to transfer many data when you call another activity.
Use the slightly modified second approach. First start the second activity, then call the AsyncTask(in onCreate method) to load the data.

Well, I think the best is starting AsyncTask on clicking button and process data in doInBackground as You said. In onCreate() of second acivity display progress bar, and in doInBackground() send data and display items. It seems to be the best from point of performance.

Related

Should I retrieve records from a database in the onResume() call?

The home page of my app displays a list of records. Another page allows adding a new record. After creating a new record, on hitting the save button, when the app goes back to the home page, I want the latest record to also be displayed.
Is there a better way of retrieving all the records rather than fetching them all from the SQLite database in a call inside onResume()?
You can save the created model in a background job/thread and then call finish() only after the save is done. You can also display a loader in the meantime.
After that, in your home Activity/Fragment (?), do another background job (in order to not freeze the UI) to retrieve the data from the SQLite. When the job is done, display it. Again, you can always display a loader while the task is running.
Simple way : have a look at AsyncTask

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.

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.

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