Skip to content Skip to sidebar Skip to footer

Difference Between Calling Getactivity().startactivity(); And Startactivity(); Inside Fragment

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

Solution 1:

Nothing. The Fragment docs are pretty clear on this:

public void startActivity (Intent intent)

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


Solution 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.


Post a Comment for "Difference Between Calling Getactivity().startactivity(); And Startactivity(); Inside Fragment"