Difference between calling getActivity().startActivity(); and startActivity(); inside Fragment

眉间皱痕 提交于 2021-02-10 09:53:27

问题


start Activity from inside a Fragment can be done with both codes :

startActivity(Intent);

getActivity().startActivity();

what is the difference ??

Note : I know the difference when it becomes to startActivityForResult(); so please don't post any thing relates to this method.

Thanks


回答1:


Nothing. The Fragment docs are pretty clear on this:

public void startActivity (Intent intent)

Call startActivity(Intent) from the fragment's containing Activity.




回答2:


The biggest difference:

Fragment#startActivity() check if mHost == null throw "Fragment this is not attached to Activity" exception. If you use getActivity().startActivity() it will return a nullable value. If mHost is null you will get NullPointException i.



来源:https://stackoverflow.com/questions/19759668/difference-between-calling-getactivity-startactivity-and-startactivity-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!