86

Is it in any way possible to launch an activity from the main function without having a UI? i.e. is there a way to create a sort of "wrapper" around another activity, i.e. by launching the main activity, it takes you to another activity automatically.

If that is not possible, is there a way to remove the main activity from the stack so that clicking the back button does not take you to a blank UI? Here's an example of what I'm trying to do:

public class WrapperActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:555-1212"));
        startActivity(intent);
    }
}
fjmustak
  • 963
  • 1
  • 7
  • 4

11 Answers11

137

Android also provides a theme specifically for this:

android:theme="@android:style/Theme.NoDisplay"
yanchenko
  • 56,576
  • 33
  • 147
  • 165
Justin
  • 6,564
  • 6
  • 37
  • 34
  • 6
    This works well, but also causes the icon to disappear from the app drawer. To get the icon to appear, you'll need to use Brian515's answer. – Learn OpenGL ES Dec 10 '12 at 23:53
  • 1
    I also found that this works, but if I display a progress dialog then it defaults to Holo.Dark which doesn't match the style of the rest of my app. – Andy Johnson Sep 12 '13 at 11:25
  • 1
    @LearnOpenGLES I'm using this theme and I still get the icon in the app drawer. I also find it to be a better option, since it sets a bunch of values to NULL, leading me to think that it is more efficient. https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml/refs/heads/master/core/res/res/values/themes.xml – givanse Nov 10 '13 at 21:28
  • @AndyJohnson You can style your progress dialogs when you create them, so you could default it Holo.Light (or any other theme) if you want. – Justin Nov 12 '13 at 23:00
  • @LearnOpenGLES This question has nothing to do with the icon being in the app drawer, your link is broken, and setting values to NULL doesn't generally make things more efficient... – Justin Nov 12 '13 at 23:01
  • Do Not Forget to put `finish()` at the end, otherwise acts weird. – Jemshit Jul 10 '15 at 14:26
  • 4
    For this to work, your activity has to inherit from Activity, not AppCompactActivity. – Kof Jul 24 '15 at 21:23
  • 4
    @Kof: you can also just copy the contents of `Theme.NoDisplay` (only 6 attributes) to any extension of `Theme.AppCompat.*` in order to create your own `Theme.AppCompat.NoDisplay` that works with `AppCompatActivity`. – MH. Sep 07 '15 at 15:25
  • Its not work in 6.0 Marshmellow, Can you pleas provide solution for this version? – Hardik Chauhan May 09 '16 at 06:46
  • 1
    FYI, If anyone trying to set theme as "NoDisplay" on target version higher than 23, please revisit code once again. reference Link: https://commonsware.com/blog/2015/11/02/psa-android-6p0-theme.nodisplay-regression.html – rk_P Apr 16 '18 at 16:06
55

In your manifest, when you declare the activity, use theme "@android:style/Theme.Translucent.NoTitleBar"

Ex:

<activity android:name="yourActivityName" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar">
yanchenko
  • 56,576
  • 33
  • 147
  • 165
strange quark
  • 5,205
  • 7
  • 41
  • 53
39

You need to add the Intent flag,

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Or

call "finish();" after firing the intent.

Vishwanath
  • 1,012
  • 8
  • 9
  • 7
    I'm not sure that the `FLAG_ACTIVITY_CLEAR_TOP` trick will work here, because the `Activity` being started will be from another application. `finish()` should definitely work, though. – CommonsWare Apr 24 '10 at 11:42
  • 1
    Added finish(); and Translucent.NoTitleBar to manifest. Works like a charm. – fjmustak Apr 25 '10 at 07:58
  • @Vishwanath Intent basically not support finish(). can u detailed, where i can call finish and how? – user170317 Nov 14 '12 at 15:19
  • 2
    You call `finish()` at the end of the Activity's `onCreate()` method, just before it returns. – intrepidis May 11 '15 at 20:52
14

Just in case you are using Android 6.0+ or Target SDK is 23+, having a theme android:theme = "@android:style/Theme.NoDisplay" will lead to a crash with error did not call finish() prior to onResume() completing. This in fact is a bug recognised by Google developers here.

So it is recommended to use an activity with following theme as a workaround.

android:theme = "@android:style/Theme.Translucent.NoTitleBar"

arunwithasmile
  • 300
  • 4
  • 16
  • you can start a thread or runnable or service or whatever and call finish in onCreate – SAIR Dec 29 '17 at 17:19
8

I think this would help you a lot:

<activity  android:name = "MyActivity" 
          android:label = "@string/app_name" 
          android:theme = "@android:style/Theme.NoDisplay" >
Steve Blackwell
  • 5,904
  • 32
  • 49
Fermin Genao
  • 81
  • 1
  • 2
8

Using

<activity android:name="yourActivityName" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar">

mentioned by Brian515 works great. This method is useful for creating an entry point Activity that decides on which activity to call, start, services, etc without having to show a UI to the user. Remember to use finish() after you have started your intent.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
Tmac
  • 3,532
  • 2
  • 18
  • 9
5

I am using AppCompatActivity and the solutions provided in this SO did not solve my problem. Here is what worked for me.

I added the following in my styles.xml.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
</style>

<style name="AppTheme.NoDisplay">
    <item name="android:windowBackground">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:windowDisablePreview">true</item>
    <item name="android:windowNoDisplay">true</item>
</style>

Then, for any activity that I want to disable the display, I modified like so:

<activity 
    android:name=".NoDisplayActivity"
    android:theme="@style/AppTheme.NoDisplay">

Cheers!

user1506104
  • 6,554
  • 4
  • 71
  • 89
4

In your manifest add @android:style/Theme.Translucent.NoTitleBar" as mentioned in some of the answers above.

Also remove the setContentView(R.layout.your_activity); line from your activity.java file.

Vipul Behl
  • 644
  • 1
  • 7
  • 20
3

I had used moveTaskToBack(true) in onResume() to put the entire activity stack in background.

Sled
  • 18,541
  • 27
  • 119
  • 168
Tarak
  • 41
  • 4
  • 1
    I had to use this method because I want the activity to show some times but not other times. – Randy Dec 04 '16 at 15:26
2

Looks similar to the question asked here: Removing an activity from the history stack

If it is, then you can use:

FLAG_ACTIVITY_NO_HISTORY

This should work to wipe activities off of the stack.

If you need to exclude from recent apps (long press home key) you can use this flag:

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

Community
  • 1
  • 1
Sreedevi J
  • 673
  • 1
  • 9
  • 15
  • This has nothing to with removing an activity from the history stack. – Justin Nov 12 '13 at 23:02
  • @Justin I'm quite sure that the question has this part: "If that is not possible, is there a way to remove the main activity from the stack so that clicking the back button does not take you to a blank UI?" Which I believe, is rather closely tied to the history stack, but I could be mistaken. – Sreedevi J Nov 14 '13 at 04:17
  • I apologize... I didn't notice the last two sentences in the question. In any case, I don't think this solves the problem. I may be mistaken but I don't think either of these flags would remove the activity from the back stack (e.g. pressing the back button would still take the user to the blank UI) – Justin Nov 26 '13 at 18:54
  • @Justin, no, it doesn't take user back to a blank UI. This flag or the manifest equivalent will not add the activities to the stack, so there is nothing to go back to. – Sreedevi J Nov 28 '13 at 03:07
2

If you are not interacting with the UI, what you are trying to do sounds more like an android service.

sebastianf182
  • 9,844
  • 3
  • 34
  • 66
  • 3
    In some cases yes.... but not in all cases. It seemed to be something that came up often-enough that the Android framework engineers created a specific theme for activities that don't have a UI. – Justin Feb 01 '12 at 19:43
  • If I use, for instance, a third-party library for selecting fonts or colors, it is typically designed to require the Activity that launches it to implement some callback interface. But I may be launching the color picker from a popup window. If I want to encapsulate the entire flow in the code for my popup window, rather than polluting my main Activity's interface, then I need a temporary (invisible) activity to act as the listener for the color chooser – Stevey Mar 23 '17 at 20:53