简体   繁体   English

片段中的Android getActivity(),getApplicationContext()

[英]Android getActivity(), getApplicationContext() in fragment

what is the correct usage of getActivity() and getApplicationContext() in fragment. 片段中getActivity()getApplicationContext()的正确用法是什么?

when getting an intent in fragment you use getActivity() and someplaces you use getAppliationContext(); 在片段中获取意图时,请使用getActivity() getAppliationContext();地方,请使用getAppliationContext();

getActivity() and getApplicationContext() getActivity()getApplicationContext()

It seems like you need to refer some document for the use of both but It's okay you can start with below links. 似乎您需要同时参考一些文档才能使用这两个文档,但是可以从下面的链接开始。

getActivity() returns the Activity the fragment is associated with. getActivity()返回与片段关联的Activity。

http://developer.android.com/reference/android/app/Fragment.html#getActivity() http://developer.android.com/reference/android/app/Fragment.html#getActivity()

getApplicationContext() returns the global Application context. getApplicationContext()返回全局Application上下文。

http://developer.android.com/reference/android/content/Context.html#getApplicationContext() http://developer.android.com/reference/android/content/Context.html#getApplicationContext()

the application context should be used for functions like receivers, functions that should be destroyed when the application is destroyed, not when the activity is destroyed. 应将应用程序上下文用于接收器之类的功能,应在销毁应用程序时销毁这些功能,而不是在活动被销毁时销毁。

getActivity()

This method gives the context of the Activity. 此方法提供了活动的上下文。 You can use it is like the yourActivity.this. 您可以使用它,就像yourActivity.this。 The method getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated. 通常在片段中使用方法getActivity()来获取将其插入或膨胀的活动的上下文。

getApplicationContext()

Returns the context for the entire application (the process all the Activities are running inside of). 返回整个应用程序的上下文(所有Activity在其中运行的进程)。 Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity. 如果您需要一个与整个应用程序的生命周期相关联的上下文,而不仅仅是当前的Activity,请使用它代替当前的Activity上下文。

For more Info . 有关更多信息

getApplicationContext() used when you want the thing beyond the scope of your fragment or lets say activity . 当您希望事物超出片段范围或可以说activity时,可以使用getApplicationContext()。

While creating singleton object or initialising library we always stick to application context . 在创建单例对象或初始化库时,我们始终坚持使用应用程序上下文。 If we tried using get activity here , it may lead to memory leaks. 如果我们在此处尝试使用get活动,可能会导致内存泄漏。

coming to getActivity() ,this work as a context but it sticks to the life cycle of your fragment . 进入getActivity()时,它可以作为上下文使用,但它会遵循片段的生命周期。 While using this as context , always make sure to put check if this is null or not . 使用此方法作为上下文时,请始终确保检查是否为null。

You can get the clear idea using below link : https://blog.mindorks.com/understanding-context-in-android-application-330913e32514 您可以使用以下链接获得清晰的主意: https : //blog.mindorks.com/understanding-context-in-android-application-330913e32514

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 onCreateView中的Android片段getActivity()。getApplicationContext()为null - Android Fragment getActivity().getApplicationContext() in onCreateView is null 哪种方法更好地编码getActivity()和getApplicationContext()? - Which is a better way to code getActivity() , getApplicationContext()? mContext.get()。getApplicationContext()和getActivity()之间的区别 - Difference between mContext.get().getApplicationContext() and getActivity() Android片段中的Volley请求队列(getApplicationContext可能会产生NullPointerException) - Volley Request Queue in Android Fragment (getApplicationContext may produce NullPointerException) 来自片段类的getApplicationContext() - getApplicationContext() from fragment class 片段getActivity无法正常工作 - Fragment getActivity not working getActivity() 或 getContext() 用于带有 viewpager 的片段? - getActivity() or getContext() for a fragment with a viewpager? 方法 getApplicationContext() 未定义 - 片段问题 - The method getApplicationContext() is undefined - fragment issues Android:从Activity调用片段函数,该片段函数包含getActivity()并返回null - Android: Call fragment functions from Activity, the fragment function contains getActivity() and returns null Android Studio getApplicationcontext() - Android Studio getApplicationcontext()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM