1

I have an android app that simply just toggles a value in the settings. For this app when clicked I DON'T want to show a layout, just a Toast and then kill itself. I have this already:

public class Test extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     ...

    Toast.makeText(this, "MESSAGE", Toast.LENGTH_LONG).show();

    android.os.Process.killProcess(android.os.Process.myPid());

}
}

Is there a possibility to disable the layout at all?

wasp256
  • 5,943
  • 12
  • 72
  • 119
  • 2
    possible duplicate of [Launch android application without main activity and start service on launching application](http://stackoverflow.com/questions/10909683/launch-android-application-without-main-activity-and-start-service-on-launching). Since your work is simple, you may not need to have/start a service. An invisible activity may be sufficient. – kabuko Aug 01 '13 at 19:38

2 Answers2

2

It sounds like a Service will be much more appropriate in this case.

Services run in the background, but can still perform some limited UI tasks such as showing Toasts.

Activities are not designed to be used without a UI.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Maybe, but it's a toggle, so there needs to be a way to initiate the intent to start the `Service` by the user. This probably means either an `Activity` so that there's a launcher icon, or I suppose a widget. An invisible `Activity` would probably be the simplest option. – kabuko Aug 01 '13 at 19:42
  • yes I would still need to initiate the Service by an Activity...@kabuko how do I set a invisible Activity? – wasp256 Aug 01 '13 at 19:53
0

Simple, just comment out one line looks like:

//setContentView(R.layout.activity_main);

oh, to kill self, you may simply call finish by the end of onCreate.

pinxue
  • 1,736
  • 12
  • 17