2

I can show resource in progressdialog three different way.

pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));

pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));

pDialog.setMessage(getResources().getString(R.string.please_wait));

Which one is the true way and why ?

Levent Tulun
  • 701
  • 3
  • 9
  • 22

1 Answers1

4

There is no true way. You only need Context:

  • getResources() when you are in Activity class (or MyActivity.this.getResources() when you are calling from inner class, while being in Activity class)
  • getActivity().getResources() (or even getContext().getResources()) when you are in Fragment class
  • context.getResources() when you pass Context through parameter
  • view.getContext().getResources() when you get Context from your View

The following two have "equivalent Context", since you are in Fragment like I can assume by your question tag:

pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));

pDialog.setMessage(getResources().getString(R.string.please_wait));

where

pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));

you get Context of entrie application.

Here some reference:

Difference between getContext() , getApplicationContext() , getBaseContext() and “this”

Community
  • 1
  • 1
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51