Calling startActivity() from outside of an Activity?
Solution 1
if your android version is below Android - 6 then you need to add this line otherwise it will work above Android - 6.
...
Intent i = new Intent(this, Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...
Solution 2
For Multiple Instance of the same activity , use the following snippet,
Note : This snippet, I am using outside of my Activity
. Make sure your AndroidManifest
file doesn't contain android:launchMode="singleTop|singleInstance"
. if needed, you can change it to android:launchMode="standard"
.
Intent i = new Intent().setClass(mActivity.getApplication(), TestUserProfileScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
// Launch the new activity and add the additional flags to the intent
mActivity.getApplication().startActivity(i);
This works fine for me. Hope, this saves times for someone. If anybody finds a better way, please share with us.
Solution 3
Android Doc says -
FLAG_ACTIVITY_NEW_TASK requirement is now enforced
With Android 9, you cannot start an activity from a non-activity context unless you pass the intent flag FLAG_ACTIVITY_NEW_TASK. If you attempt to start an activity without passing this flag, the activity does not start, and the system prints a message to the log.
Note: The flag requirement has always been the intended behavior, and was enforced on versions lower than Android 7.0 (API level 24). A bug in Android 7.0 prevented the flag requirement from being enforced.
That means for (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
it is mandatory to add Intent.FLAG_ACTIVITY_NEW_TASK
while calling startActivity()
from outside of an Activity
context.
So it is better to add flag for all the versions -
...
Intent i = new Intent(this, Wakeup.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
...
Solution 4
You didn't paste the part where you call startActivity
, that's the interesting part.
You might be calling startActivity
in a Service
context, or in an Application
context.
Print "this" to log cat before making the startActivity
call, and see what it refers to, it's sometimes a case of using an inner "this" accidentally.
Solution 5
Sometimes this error can occur without an explicit call to startActivity(...)
. For example, some of you may have seen a stack trace like this in Crashlytics:
Fatal Exception: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
at android.text.style.URLSpan.onClick(URLSpan.java:62)
at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
at android.widget.TextView.onTouchEvent(TextView.java:9522)
at android.view.View.dispatchTouchEvent(View.java:8968)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5303)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2559)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1767)
at android.app.Activity.dispatchTouchEvent(Activity.java:2866)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2520)
at android.view.View.dispatchPointerEvent(View.java:9173)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4706)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4544)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4201)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4258)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6564)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6454)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6425)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6654)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
And you may wonder what you did wrong, since the trace only includes framework code. Well, here's an example of how this can happen. Let's say we're in a fragment.
Activity activity = getActivity();
Context activityContext = activity;
Context appContext = activityContext.getApplicationContext();
LayoutInflater inflater = LayoutInflater.from(appContext); // whoops!
View view = inflater.inflate(R.layout.some_layout, parent, false);
TextView tvWithLinks = (TextView) view.findViewById(R.id.tv_with_links);
tvWithLinks.setMovementMethod(LinkMovementMethod.getInstance()); // whoops!!
Now, when a user clicks on that text view, your app will crash with the stack trace above. This is because the layout inflater has a reference to the application context, and so therefore your text view has an application context. Clicking on that text view implicitly calls appContext.startActivity(...)
.
Final note: I tested this on Android 4, 5, 6, and 7 devices. It only affects 4, 5, and 6. Android 7 devices apparently have no trouble calling appContext.startActivity(...)
.
I hope this helps someone else!
Related videos on Youtube
Tom G
Updated on July 08, 2022Comments
-
Tom G almost 2 years
I'm using an
AlarmManager
to trigger an intent that broadcasts a signal. The following is my code:AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(this, Wakeup.class); try { PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0); Long elapsed += // sleep time; mgr.set(AlarmManager.RTC_WAKEUP, elapsed, pi); } catch(Exception r) { Log.v(TAG, "RunTimeException: " + r); }
I'm calling this code from an
Activity
, so I don't know how I could be getting the following error...ERROR/AndroidRuntime(7557): java.lang.RuntimeException: Unable to start receiver com.wcc.Wakeup: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
-
Ovidiu Latcu over 12 yearsyes this works. but if you want to start multiple instances of the same Activity this does not work. Since the FLAG_ACTIVITY_NEW_TASK will resume the same existing Activity
-
Pavankumar Vijapur over 12 yearsAdd flag FLAG_ACTIVITY_Multiple_TASKS TO RESOLVE YOUR problem
-
JehandadK over 12 yearsPlease understand the difference between setFlags() and addFlag() methods. All you are doing now is launching an activity with FLAG_ACTIVITY_NEW_TASK. As in the code posted by Cristian below.
-
JehandadK over 12 years-------------------------------------------------------------------i.addFlag(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); i.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK); ------------------------------------------------------------------- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
-
Hazen Hills Software over 10 yearsThis is likely the problem OP is having. I have encountered the same. Launching an activity with getApplicationContext() instead of an existing activity will trigger this error.
-
Martin Pfeffer almost 10 yearsNICE :) I'm quite sure this was the mistake I try to spot the whole evening. Thanks, Mister.
-
Bill Bell over 7 yearsI'm writing as a reviewer because your answer has been flagged for low-quality. The shortcoming of this answer is that there's no explanation of what your code does or of why it solves the problem posed in the question. Would you please add some explanatory comments, bearing in mind that many programmers could read your answer in future for guidance. Thank you!
-
Md. Shafiqur Rahman over 7 yearsWhen you wnat to open an activity within your app then you can call the startActivity() method with an Intent as parameter. That intent would be the activity that you want to open. First you have to create an object of that intent with first parameter to be the context and second parameter to be the targeted activity class. Hope this will help.
-
Bill Bell over 7 yearsOK. But rather than putting that information in a comment you should edit your answer and put it there. I regret belabouring this point but it's important to do this for every answer. Best regards.
-
kiwicomb123 almost 7 yearsYes, please include explanations as to why your answer is correct. We only learn to be better programmers when we learn WHY we do the things we do.
-
Malavan over 4 yearswhat is the solution for that
-
Vadim Kotov over 4 yearsBeware, you need this on Android 9+ also, see this answer for clarifications