7

As per this post, I know I am allowed to create an activity that has no user interface by declaring your activity like below:

<activity
    android:name=".NoUIActivity"
    android:theme="@android:style/Theme.NoDisplay">
</activity>

My problem is NoUIActivity extends AppCompatActivity. If I use the android:theme line above, it gives me error saying I should use the corresponding AppCompat theme. Help please. Thanks!

user1506104
  • 6,554
  • 4
  • 71
  • 89
  • 2
    Probably useful: https://commonsware.com/blog/2015/11/02/psa-android-6p0-theme.nodisplay-regression.html – stkent May 12 '18 at 19:47
  • 5
    If there is no UI, just extend `Activity`, instead of `AppCompatActivity`. What are you gaining from using `AppCompatActivity` in this case? Also, as the blog post that Mr. Kent pointed to mentions, you probably want `Theme.Translucent.NoTitleBar`, instead of `Theme.NoDisplay`. – CommonsWare May 12 '18 at 20:00
  • I have a prebuilt activity handler class that extends AppCompatActivity. All my other activities are subclasses of it including the non ui activity. I will just probably make an exception for this one. – user1506104 May 13 '18 at 04:08

2 Answers2

1

Your activity should look like:

public class NoUIActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_no_uiactivity);
    }
}

And in AndroidManifest.xml declare activity like:

<activity
            android:name=".NoUIActivity"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"
            android:exported="false" />
Amol Desai
  • 872
  • 1
  • 9
  • 17
0
  1. Create an app with single activity
  2. Delete the layout xml which you don't need
  3. Remove setContentView line (generated by Android Studio),in Activity's onCreate() method
  4. Call finish() when done
Syscall
  • 19,327
  • 10
  • 37
  • 52
Abolee
  • 36
  • 2