【Android】报错android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity

In the process of implementing a floating ball function (let the interface hours after the floating ball pops up, and click the floating ball to return to the original interface), an error was reported:
error:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Solution:
There is a startActivity method in Context. Activity inherits from Context and overloads the startActivity method.

If you use the startActivity method of Activity, there are no restrictions, and if you use the startActivity method of Context, you need to start a new task. The exception above is all because of the use of the startActivity method of Context.

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

Guess you like

Origin blog.csdn.net/qq_30885821/article/details/108883405