5

I'm trying to write a very simple app that will do just one very simple non-GUI action, then display a short message (using toast, on top of what was already on the screen, e.g. home screen), and finish. That's all.

This almost works, but i'm still getting a brief black screen after the app starts, then back to home screen and things work as i intend after that. I thought that not using setContentView on my activity (thus depriving it of a View) would be enough to avoid the black screen, but it still pops up. Please notice that (as suggested in a related question), a service is not the answer to my problem. I just want a GUI-less (except for one toast) app that runs and ends right away. Thanks :)

2
  • 1
    It would probably be helpful if you explained why a Service is not the answer to your problem. This sounds very Service-oriented.
    – Matt
    Dec 29, 2010 at 6:20
  • 1
    First, precisely because i don't want my process to keep running in the background like services are intended to. I want quick and clean enter and exit. And second, Android documentation directly states that activities don't have to have a UI, but i hadn't found the way to do that. (Answer 1 already solved my problem) Thanks anyway.
    – shutou
    Dec 29, 2010 at 9:53

3 Answers 3

17

In <activity> in your manifest use

android:theme="@android:style/Theme.NoDisplay"

Note this assumes you will call finish() before returning from onResume(). If you are going to do more work after that (for example if you will be doing any networking or other such thing that can't be done quickly synchronously), you will probably want to start a service to take care of the work to tell the platform your process should continue running after the activity finishes.

5
  • Great! This is exactly what i needed. Precisely because i don't want my process to keep running in the background. Thanks mate!
    – shutou
    Dec 29, 2010 at 9:50
  • @shutou: This is probably (though I have no idea) more overhead than starting a service. You don't have to let the service continue running in the background. See my response to my answer.
    – Falmarri
    Dec 29, 2010 at 15:43
  • I'm using an entry activity as my launcher activity, because the actual activity needs to be determined by some logic (e.g. intro, signup, or content). Before this the entry activity would also show, which was annoying but now it's disappeared! Thanks
    – Meanman
    Feb 20, 2014 at 10:04
  • I was getting ANRs without any obvious reason on my activity with Theme.NoDisplay (I had commented out almost the complete code) and in the end your note for calling finish() helped me. Thanks a lot!
    – dragi
    Jul 30, 2014 at 11:33
  • Why do I need to call finish?
    – JohnyTex
    Jul 4, 2016 at 11:42
0

Sounds like a job for an IntentService. You send it an intent, and if it isn't running it starts, then it treats all received intents, then it stops. Short and simple.

-2

a service is not the answer to my problem

Yes it is

2
  • Alright, it may be an alternative way to do what i want, but as seen in answer, it's possible to shut off display of an app's UI. And since i don't want background processes running after my simple app is done, i figured a service may be more than what i need. I admit, though, i still don't know enough to be certain whether a service could be more efficient than the solution given in Answer 1, which allows my app to be made of only a minimal activity that doesn't span any extra processes or threads. I may learn more about services in order to consider the pros and cons in more detail. Thanks
    – shutou
    Dec 29, 2010 at 10:25
  • Services don't spawn extra processes or threads. A service is essentially an activity without a GUI. I haven't inspected the source code thoroughly, but I'll be it's almost exactly that. You don't need to leave the service running. In fact, don't do that. Just start the service, do the work, and then call stopSelf()
    – Falmarri
    Dec 29, 2010 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.