在Android中获取Fragment中的应用程序上下文?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20464273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Application Context In Fragment In Android?
提问by NRahman
I have stored some data to a Global Class By using the Application Context In One Activity. Later I have to Retrieve those values in A Fragment. I have done something like this to store in Global Class.
我通过在一个活动中使用应用程序上下文将一些数据存储到全局类。后来我必须在片段中检索这些值。我已经做了这样的事情来存储在全局类中。
AndroidGlobalClass AGC = ((AndroidGlobalClass) getApplicationContext());
AGC.setUser_access("XYZ");
AGC.setFirst_name("ABC");
And In the Manifest I have done :
在我所做的清单中:
<application
android:name=".AndroidGlobalClass"
android:theme="@style/AppTheme" >
<activity
android:name="abc.SignInActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Now When I am Trying to Get the Application Context Using this... I am not getting the Context...
现在,当我尝试使用它获取应用程序上下文时......我没有得到上下文......
AndroidGlobalClass AGC = ((AndroidGlobalClass) getApplicationContext());
This is My Fragment Activity
这是我的片段活动
public class Fragment_NewsFeed extends Fragment {
public Fragment_NewsFeed() {
}
RestImplimentationMethods RIM;
AndroidGlobalClass AGC;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_newsfeed, container, false);
return rootView;
}
}
回答by SalGad
You can get the context using
getActivity().getApplicationContext();
您可以使用获取上下文
getActivity().getApplicationContext();
回答by Aakash Goyal
Use
用
getActivity().getApplicationContext()
getActivity().getApplicationContext()
to obtain the context in any fragment
获取任何片段中的上下文
回答by Ashwin S Ashok
Try to use getActivity();
This will solve your problem.
尝试使用getActivity();
这将解决您的问题。
回答by Nabz
you can define a global variable :
您可以定义一个全局变量:
private Context globalContext = null;
and in the onCreate method, initialize it :
并在 onCreate 方法中,对其进行初始化:
globalContext = this.getActivity();
And by that you can use the "globalContext" variable in all your fragment functions/methods.
这样您就可以在所有片段函数/方法中使用“globalContext”变量。
Good luck.
祝你好运。
回答by Jithin Jude
In Support Library 27.1.0 and later, Google has introduced new methods requireContext()
and requireActivity()
methods.
在 Support Library 27.1.0 及更高版本中,Google 引入了新的方法requireContext()
和requireActivity()
方法。
Eg:ContextCompat.getColor(requireContext(), R.color.soft_gray)
例如:ContextCompat.getColor(requireContext(), R.color.soft_gray)
More info here
更多信息在这里
回答by Qasim Wani
Pretty late response but you can do the following in Kotlin:
响应很晚,但您可以在 Kotlin 中执行以下操作:
activity?.applicationContext?.let { SymptomsAdapters(it, param2, param3, ...) }
(?.) is for safe null operation to prevent from the null pointer exception.
(?.) 用于安全空操作以防止空指针异常。
You do not want to create a new context as that can lead to memory leaks when interchanging between fragments or when the device changes rotation.
您不想创建新的上下文,因为在片段之间交换或设备更改旋转时可能会导致内存泄漏。
And as someone mentioned above, in Java you can obtain context in a fragment by doing the following:
正如上面提到的,在 Java 中,您可以通过执行以下操作获取片段中的上下文:
getActivity().getApplicationContext()
回答by Joolah
Add this to onCreate
将此添加到 onCreate
// Getting application context
Context context = getActivity();