Nick
Nick

Reputation: 3494

Widget starting a service also starts main activity

I have a widget that is supposed to start and stop a service (start it when it's not running, stop it when it is). This is working fine, however, each time the service is started, my app's main activity is also launched, which I don't want. If I remove the MAIN-intent-filter ( <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
) from the app's manifest, it works as I want it to (without launching the main-activity, just the service), but then I obviously don't have a main activity anymore...

This is how I start the service (I would assume this is the normal way, and I can't see any reference to what might cause the MAIN intent to fire):

Intent svc = new Intent(this, OnOffService.class);
startService(svc);

Thanks,
Nick

Upvotes: 2

Views: 1001

Answers (3)

tamir
tamir

Reputation: 1

Add finish() to the activity after it starts the service.

Upvotes: 0

ubershmekel
ubershmekel

Reputation: 12798

Your solution is not a good one, for example onPause() occurs when the user changes orientation (rotates the cell phone), so your app is going to just disappear when that happens.

I asked and found the solution: Why does starting an activity from a widget cause my main activity to start as well?

Upvotes: 4

Nick
Nick

Reputation: 3494

I now 'solved' it by adding a finish(); to my main activity's onPause(). Not a nice solution, but the main activity isn't heavy, so reloading it (rather than getting it back from memory) shouldn't do too much harm. Still weird and I would love to know what I'm doing wrong :)!

Upvotes: 0

Related Questions