I'm just getting started with Android and was reading up BroadcastReceiver. Since the MainActivity was being used only to get the alarm time in seconds, it got me thinking whether layout XML files are must for every activity in Android. I mean, is it possible to have an app that when launched, shows no view, but successfully sets up a receiver?
-
1Thanks, but I don't think this is a general discussion, or likely to generate subjective opinions. I'm asking whether such a thing is possible or not. Guess I'll remove the "General Discussion" from my post. – ankush981 Jun 27 '13 at 14:42
-
1I think you're thinking about a service... – EMarci15 Jun 27 '13 at 14:49
-
@EMarci15: Thanks! Looks like I've got a LOT of reading to do tonight! :) – ankush981 Jun 27 '13 at 15:05
-
Similar: http://stackoverflow.com/questions/8571887/start-activity-without-showing-it – trante May 21 '14 at 08:02
-
@wtsang02 your comment pictures what most people hate nowadays on stackoverflow: people that look for closing questions whenever they can without even trying to understand why this questions has been asked or if it is really a duplicated. I would have downvoted you if only it was possible on comments and I definitely flagged you as not constructive. – Simon Ninon Jun 14 '17 at 00:36
-
@SimonNinon 1) Look at edit history. You will see what I am referring to. I am pointing out why he wrote a bad question due to his format. I didn't vote close on this. 2) 'nowadays' that comment was 4 years ago... – wtsang02 Jun 14 '17 at 00:45
2 Answers
The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.:
An activity is a single, focused thing that the user can do. Almost all activities interact with the user [...]
(see http://developer.android.com/reference/android/app/Activity.html)
Related SO question: https://stackoverflow.com/a/12817384/534471
To e.g. display a Toast from an Activity without layout you would define the activity in your manifest like so:
<activity
android:name=".MainActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The code would look like this:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "I'm alive", Toast.LENGTH_LONG).show();
finish();
}
}

- 1
- 1

- 28,488
- 11
- 69
- 85
-
Okay . . . For a start, let's just say the user clicks on the app and a Toast message is displayed (perhaps after 5 seconds). But it has to be an activity, not (as others have posted), a service. Possible? – ankush981 Jun 27 '13 at 15:07
-
Yes a Toast would be shown from an Activity but why after 5 seconds? No user wants to wait for 5 seconds... – Emanuel Moecklin Jun 27 '13 at 15:08
-
There are other ways to schedule an Alarm. If the app already has an alarm time there's no need to start an Activity but there's really no telling what you are trying to achieve. – Emanuel Moecklin Jun 27 '13 at 15:11
-
The purpose is not UX-related, but educational. Ideally, I'd like to use an AlarmManager and BroadcastReceiver, etc., but let's simplify the problem and just display a Toast. – ankush981 Jun 27 '13 at 15:12
-
It seems to me that notifications could be better suited to your problem. If you just want to inform the user that an alarm went of then that would be the way to go. – Emanuel Moecklin Jun 27 '13 at 15:14
-
I'd like to repeat that I'm NOT concerned about the user here. Having coded every Activity with a corresponding UI so far, I'd very much like to see one with none: An invisible Activity that does something to prove its existence. Now, is that too much to ask for? :) – ankush981 Jun 27 '13 at 15:16
-
I updated my answer to show you how to display a toast in an Activity which doesn't use a layout – Emanuel Moecklin Jun 27 '13 at 15:30
-
Absolutely delightful! That's the most brilliant thing I've seen for a long time! Thank you so much; this will fill a major gap in my understanding. – ankush981 Jun 27 '13 at 16:23
-
I think this answer misunderstands the meaning of a 'background activity'. such an activity would typically have a UI, but it would not be *currently* visible to the user. – Sam Sep 16 '15 at 12:53
You can implement an Activity without a UI. In the manifest you can specify android:theme="@android:style/Theme.NoDisplay"
. Take a look at this
You can also implement a Service which does not have any UI so you do not need layout inflation. Service just runs in background and shows no views. Take a look at Android Training and API Guide to learn more about Services

- 1,326
- 7
- 15
-
1
-
Use `Theme.Translucent` instead of `Theme.NoDisplay`. See [here](https://web.archive.org/web/20151116170752/https://code.google.com/p/android-developer-preview/issues/detail?id=2353) – Sourav Kannantha B May 22 '21 at 07:54