简体   繁体   中英

How to launch an Activity without a UI?

Is it in any way possible to launch an activity from the main function without having a UI? ie is there a way to create a sort of "wrapper" around another activity, ie 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 还专门为此提供了一个主题:

android:theme="@android:style/Theme.NoDisplay"

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">

You need to add the Intent flag,

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Or

call " finish(); " after firing the intent.

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"

I think this would help you a lot:

<activity  android:name = "MyActivity" 
          android:label = "@string/app_name" 
          android:theme = "@android:style/Theme.NoDisplay" >

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.

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!

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.

我在onResume()使用了moveTaskToBack(true)将整个活动堆栈置于后台。

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

如果您不与 UI 交互,那么您尝试做的事情听起来更像是一个 android 服务。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM