android - 在 fragment 中使用进度对话框显示资源

标签 android android-fragments

我可以用三种不同的方式在进度对话框中显示资源。

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));

哪一个是正确的方法?为什么?

最佳答案

没有真正的方法。您只需要Context :

  • getResources()当你在Activity时class (或 MyActivity.this.getResources() 当您在 Activity 类中从内部类调用时)
  • getActivity().getResources() (甚至 getContext().getResources() )当您处于 Fragment 时类
  • context.getResources()当你经过Context时通过参数
  • view.getContext().getResources()当你得到Context时来自您的View

以下两个具有“等效 Context ”,因为您位于 Fragment就像我可以通过你的问题标签假设的那样:

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

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

哪里

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

你得到Context条目申请。

这里有一些引用:

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

关于android - 在 fragment 中使用进度对话框显示资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34607979/

相关文章:

android - 阅读放置在android Assets 文件夹中的pdf文件

android - 我可以删除 layout.xml 文件中定义的 fragment 吗?

java - 带有 PageTransformer 的嵌套 fragment 中带有 ViewPager 的 dispatchDraw 出现 Nullpointerexception

android - 在 PreferenceFragmentCompat 内部实现 PreferenceFragmentCompat

javascript - android 上的 html 输入框点击时有绿色突出显示

android - 摇晃检测android wear

java - 蓝牙启用窗口打开

android - 在 EditText 中输入文本时 ActionBar 展开

java - 使用 Fragment 后退按钮 android

java - ViewPager 没有快速响应