Question

My application contains 2 parts: a network part and GUI. It works kinda like a browser - user requests some information from server, server sends back some data representing some view, then GUI displays it.

Right now I've implemented network part as a service, which constructs and keeps all the views by itself. When an activity is created, network part sets it's content view to the last received view.

That's pretty simple, no need to recreate all the view hierarchy and saving all the states.

But there is problem - I can create Views within service context, I can keep them between activity launches, can unbind and rebind views when an activity is destroyed and created? but some views require Activity context (e.g. Spinner, that creates popup dialog).

So, is there any way to change context of a View after creation?

I'm very new in android programming, so there may be some better way to do this (again, the task is - keep view hierarchy and all the states when activity beeing destroyed and recreated, when a screen rotating occurs, for example).

Was it helpful?

Solution

I strongly believe you are on the wrong way with "I've implemented network part as a service, which constructs and keeps all the views by itself.".

It is the Activity only that is intended to have/manage Views hierarchy. So an Activity should ask a Service (or be somehow notified by the Service) for the data to display and then just update its views. Yes, in some cases it requires state management, but the benefit you get - it just works as intended without any hacks. In other words - don't try to cheat the OS, instead try to correspond to the best practices for Android.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top