android - Should I use a Service as a way of accessing network data? -


i'm new android development , i'm trying build simple app browse network dlna servers , allow me browse each dlna server directory. thought pretty simple: 1 activity displays dlna servers on network, user selects one, redirect activity browses dlna media server's directories.

my question quite basic: how persist information 'selected server', 'current directory' , other things this? need way rest of application know do. should build data service? seems violates common understanding of services should be. how can i/should persist application state?

you have few options. 1 think best pass these values in intent object you're sending 1 activity next. therefore, activity browses dlna media server's directories receives selected server in intent, , if came dlna list activity, that's great, intent come other activity wants show media server directories user. try hard encapsulate functionality inside activity doesn't need depend on rest of application data.

when there data need across application, take @ sharedpreferences. these can displayable user can make choices persist across invocations of application, , activity in application can read preferences , keep current. not preferences need displayable however. can create preferences editor read , write own preferences without ever showing them user.

a couple of other options setup singleton classes hold shared values, , every activity can refer to, or last resort can extend application class, , create getters , setters make values available across application. last resort because application significant class in android that's better left alone don't accidentally break or in future.

to answer other question, not should use services network accesses, need create threads well. android service not default include background thread, need yourself. should never make network calls main (a.k.a. ui) thread.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -