How to launch an Activity without a UI?

后端 未结 11 1641
失恋的感觉
失恋的感觉 2020-11-30 17:48

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.

相关标签:
11条回答
  • 2020-11-30 18:36

    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

    0 讨论(0)
  • 2020-11-30 18:37

    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.

    0 讨论(0)
  • 2020-11-30 18:39

    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.

    0 讨论(0)
  • 2020-11-30 18:42

    Android also provides a theme specifically for this:

    android:theme="@android:style/Theme.NoDisplay"
    
    0 讨论(0)
  • 2020-11-30 18:49

    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!

    0 讨论(0)
提交回复
热议问题