How to launch an Activity without a UI?
AndroidAndroid Problem Overview
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);
}
}
Android Solutions
Solution 1 - Android
Android also provides a theme specifically for this:
android:theme="@android:style/Theme.NoDisplay"
Solution 2 - Android
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">
Solution 3 - Android
You need to add the Intent flag,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Or
call "finish();
" after firing the intent.
Solution 4 - Android
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"
Solution 5 - Android
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.
Solution 6 - Android
I think this would help you a lot:
<activity android:name = "MyActivity"
android:label = "@string/app_name"
android:theme = "@android:style/Theme.NoDisplay" >
Solution 7 - Android
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!
Solution 8 - Android
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.
Solution 9 - Android
I had used moveTaskToBack(true)
in onResume()
to put the entire activity stack in background.
Solution 10 - Android
Looks similar to the question asked here: https://stackoverflow.com/questions/1898886/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
Solution 11 - Android
If you are not interacting with the UI, what you are trying to do sounds more like an android service.