问题
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