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.
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
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.
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.
Android also provides a theme specifically for this:
android:theme="@android:style/Theme.NoDisplay"
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!