Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No analog to the Service#onStartCommand() lifecycle method? #27

Closed
curioustechizen opened this issue Oct 3, 2012 · 8 comments
Closed
Assignees
Labels
Milestone

Comments

@curioustechizen
Copy link

Going through the documentation for @Service, I see that the following lifecycle events are defined for a Service

@OnCreate
@OnStart
@OnDestroy

However, onStart() is deprecated and one should now use onStartCommand(). I do not see any direct analog to onStartCommand() in transfuse. Is this intended?

@johncarl81
Copy link
Owner

@curioustechizen, great catch.

So, what do you say we remove the Service mapping of @OnStart (to support the depreciation) and introduce a new @OnStartCommand annotation and related Service mapping? I suppose we could map the @OnStart to the onStartCommand() method instead of removing it. Thoughts?

@johncarl81 johncarl81 reopened this Oct 3, 2012
@johncarl81
Copy link
Owner

On second though, this may be a case for a call-through event method because of the return value out of onStartCommand().

@ghost ghost assigned johncarl81 Oct 3, 2012
@curioustechizen
Copy link
Author

I had forgotten about the return value of onStartCommand() when I originally opened this issue! Well, I think this is definitely a case for call-through event. Something like ServiceOnStartCommand. While you're at it, you might as well add binding support :-) (or maybe that qualifies as an advanced use-case where the developer should sub-class Service anyway?)

@johncarl81
Copy link
Owner

Ok, I'll add ServiceOnStartCommand shortly.

YES, automatic binding (on injection?) would be awesome. It may be tricky to get right though, needs some thought.

@curioustechizen
Copy link
Author

Just to clarify: The idiom for creating a started Service is as follows:

    class MyService extends Service{
    @Override
    public IBinder onBind(Intent intent){
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        //Do Something
    }

    @Override
    public void onDestroy(){
        //Do Some cleanup
    }

}

In other words, since onBind() is declared as abstract in Service, you must implement it in your service and return null for a started service. Is this how the code generated by transfuse would look like?

I will think about bound services in greater detail and open a new feature request if I find it feasible.

@johncarl81
Copy link
Owner

Yes, that is what the code generated by Transfuse would look like.
This:

@Service
@RegisterListener
public class Example implements ServiceOnStartCommand{

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return return android.app.Service.START_STICKY;
    }

    @OnDestroy
    public void cleanup(){
        //Do some cleanup
    }
}

triggers Transfuse to generate this:

@Generated(value = "org.androidtransfuse.TransfuseAnnotationProcessor", date = "10/4/12 7:45 AM")
public class ExampleService extends Service implements ContextScopeHolder
{

    private Example example_0;
    private Scope scope_10 = new ConcurrentDoubleLockingScope();

    public void onCreate() {
        super.onCreate();
        example_0 = new Example();
    }

    public void onDestroy() {
        super.onDestroy();
        example_0 .cleanup();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    public Scope getScope() {
        return scope_10;
    }
}

johncarl81 added a commit that referenced this issue Oct 5, 2012
johncarl81 added a commit that referenced this issue Oct 5, 2012
Added additional missing Service lifecycle and call-through events
@johncarl81
Copy link
Owner

@curioustechizen
Copy link
Author

Thanks for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants