Asked by ABAINEH
Is it possible to have activity without UI to perform action?A. not possible B.wrong question C.Yes,it is possible D,none
All Answers 1
Answered by
GPT-5 mini
AI
Answer: C. Yes, it is possible.
Explanation and notes:
- An Activity can have no visible UI (for example by not calling setContentView or by using the Theme.NoDisplay theme). This is useful for doing a quick task that needs an Activity context (e.g., handle an intent, request permissions, show a transient dialog) and then finish().
- Typical pattern: use android:theme="@android:style/Theme.NoDisplay" in the manifest for that activity, perform work in onCreate/onResume, then call finish().
Example (Java):
```java
public class NoUiActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// perform action here
doWork();
finish();
}
}
```
Manifest:
```xml
<activity android:name=".NoUiActivity"
android:theme="@android:style/Theme.NoDisplay" />
```
Recommendation: for background or long-running tasks, prefer Service, WorkManager, or JobScheduler instead of an Activity.
Explanation and notes:
- An Activity can have no visible UI (for example by not calling setContentView or by using the Theme.NoDisplay theme). This is useful for doing a quick task that needs an Activity context (e.g., handle an intent, request permissions, show a transient dialog) and then finish().
- Typical pattern: use android:theme="@android:style/Theme.NoDisplay" in the manifest for that activity, perform work in onCreate/onResume, then call finish().
Example (Java):
```java
public class NoUiActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// perform action here
doWork();
finish();
}
}
```
Manifest:
```xml
<activity android:name=".NoUiActivity"
android:theme="@android:style/Theme.NoDisplay" />
```
Recommendation: for background or long-running tasks, prefer Service, WorkManager, or JobScheduler instead of an Activity.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.