不同Context的应用场景:
image.png
NO1:Application和Service可以启动一个Activity,需要创建一个新task,比如Application调用startActivity(intent)系统会报错误:
java.lang.RuntimeException: Unable to create application
com.xjp.toucheventdemo.MyApplication: android.util.AndroidRuntimeException: Calling
startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
flag. Is this really what you want?
除了Activity可以创建一个Dialog,其他都可以创建Dialog会报错
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
其实可以写全局的dialog不需要依附Activity,可以用Application的Context来显示系统级别的dialog。
网友评论