How to start an activity without a user interface?

Is there any way to start activity from the main function without a user interface? that is, there is a way to create a kind of โ€œwrapperโ€ around another action, that is, by starting the main action, it will automatically switch to another activity.

If this is not possible, is there a way to remove the main action from the stack so that clicking the back button does not lead you to an empty interface? Here is 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); } } 
+55
android
Apr 24 '10 at 10:23
source share
10 answers

You need to add an Intent flag,

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

or

call " finish(); " after the intent is fired.

+24
Apr 24 '10 at 11:26
source share

Android also provides a theme specifically for this:

 android:theme="@android:style/Theme.NoDisplay" 
+112
03 Feb 2018-11-22T00:
source share

In your manifest, when you declare an action, use the topic "@android:style/Theme.Translucent.NoTitleBar"

Example:

 <activity android:name="yourActivityName" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent.NoTitleBar"> 
+50
Apr 24 '10 at 14:31
source share

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 Activity entry point, which determines which activity to call, launch, services, etc., without having to show the user interface to the user. Remember to use finish() after you begin your intent.

+7
Sep 30 '10 at 15:14
source share

I think this will help you a lot:

 <activity android:name = "MyActivity" android:label = "@string/app_name" android:theme = "@android:style/Theme.NoDisplay" > 
+7
Feb 16 '15 at 0:34
source share

Just in case, if you use Android 6.0+ or โ€‹โ€‹Target SDK 23+, the theme android:theme = "@android:style/Theme.NoDisplay" will crash with the error did not call finish() prior to onResume() completing . This is actually a bug recognized by Google developers here .

Therefore, it is recommended that you use the action with the following topic as a workaround.

android:theme = "@android:style/Theme.Translucent.NoTitleBar"

+6
Jun 15 '17 at 10:04 on
source share

Similar to the question asked here: Removing activity from the history stack

If so, you can use:

FLAG_ACTIVITY_NO_HISTORY

This should work to erase actions from the stack.

If you need to exclude from the latest applications (long press the home key), you can use this flag:

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

+2
Oct 26 '11 at 5:17
source share

I used moveTaskToBack(true) in onResume() to put the entire activity stack in the background.

+2
Aug 17 '13 at 22:49
source share

In your manifest, add @android:style/Theme.Translucent.NoTitleBar" as mentioned in some of the answers above.

Also remove the line setContentView(R.layout.your_activity); from activity.java file.

+2
Aug 05 '15 at 16:51
source share

If you are not interacting with the user interface, then what you are trying to do is more like an Android service.

+1
Dec 26 2018-11-21T00:
source share



All Articles