The widget starting the service also starts the main activity.

I have a widget that should start and stop the service (start it when it is not working, stop it when it is). This works fine, however, every time the service starts, the main application of my application also starts, which I don’t want. If I remove the MAIN-aim-filter ( <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
) from the application manifest, it works the way I want (without starting the main activity, just a service), but then I obviously no longer have the main activity ...

This is how I start the service (I would suggest that this is the normal path, and I see no reference to what might lead to the main intention):

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

Thanks,
Nick

+2
source share
3 answers

Your solution is not very good, for example onPause () happens when the user changes orientation (rotates the cell phone), so your application will simply disappear when this happens.

I asked and found a solution: Why does launching an activity from a widget also trigger the start of my main activity?

+4
source

Now I solved it by adding finish(); to my main activity onPause() . This is not a good solution, but the main activity is not heavy, so rebooting (and not returning it from memory) should not do too much harm. Still strange, and I would like to know what I'm doing wrong :)!

0
source

Add finish() to the action after starting the service.

0
source

Source: https://habr.com/ru/post/1333426/


All Articles