简体   繁体   中英

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM