Toast in Fragment, should use getActivity() or getAcitivity().getApplicationContext()?

android

Solution

For user interface related calls use the Activity context.

See this explanation by Reto Meier: https://stackoverflow.com/a/987503/534471

Problem

Sorry for my newbie Question, i just cannot find the answer from google and stackoverflow.. i just start learning for android, wish can build a good base for android knowledge. I wondering which i should use in the following if i create toast.maketext in fragment. getActivity() or getAcitivity().getApplicationContext()? i did try both, it works well.. ``` btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "hello",Toast.LENGTH_LONG).show(); Toast.makeText(getActivity().getApplicationContext(),"Hello",Toast.LENGTH_LONG).show(); } }); ```

Original source

Related problems