本文介绍了Activity.getIntent()是否可以返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Activity.getIntent() 可以返回null吗?
文档中没有提到这种可能性,因此我想知道是否必须在取消引用null值之前检查getIntent()的结果.
The documentation does not mention this as a possibility, so I am wondering if I have to check the result of getIntent() for null value before dereferencing it.
推荐答案是的,它可以, 但只有两种情况:
Yes, it can, but only in two cases:
在活动构造函数中: 在内部attach方法中设置的意图,从Instrumentation类调用:
In activity constructor: Intent set up in internal attach method, called from Instrumentation class:
public Activity newActivity(Class<?> clazz, Context context, IBinder token, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, Object lastNonConfigurationInstance) throws InstantiationException, IllegalAccessException { Activity activity = (Activity)clazz.newInstance(); ActivityThread aThread = null; activity.attach(context, aThread, this, token, 0, application, intent, info, title, parent, id, (Activity.NonConfigurationInstances)lastNonConfigurationInstance, new Configuration(), null, null); return activity; }因此,意图在构造函数中始终为空.
therefore intent is always null in constructor.
setIntent(null)之后: 可以通过 setIntent() .
After setIntent(null): It's possible to change intent from outside of activity with setIntent().
在所有其他情况下都不能.
更多推荐
Activity.getIntent()是否可以返回null?
发布评论