Java 如何将活动上下文放入非活动类android?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22371124/
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
How to get activity context into a non-activity class android?
提问by user3354605
I have a Activity class from where I am passing some information to a helper class(Non-activity) class. In the helper class I want to use the getSharedPreferences()
. But I am unable to use it as it requires the activity context.
我有一个 Activity 类,我将一些信息传递给辅助类(非活动)类。在助手类中,我想使用getSharedPreferences()
. 但我无法使用它,因为它需要活动上下文。
here is my code:
这是我的代码:
class myActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Info = new Authenticate().execute(ContentString).get();
ItemsStore.SetItems(Info);
}
}
class ItemsStore
{
public void SetItems(Information info)
{
SharedPreferences localSettings = mContext.getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localSettings.edit();
editor.putString("Url", info.Url);
editor.putString("Email", info.Email);
}
}
ANy idea how this can be achieved?
知道如何实现吗?
采纳答案by Mohammad Ashfaq
Try this:
尝试这个:
class myActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Info = new Authenticate().execute(ContentString).get();
ItemsStore.SetItems(Info, getApplicationContext());
}
}
class ItemsStore
{
public void SetItems(Information info, Context mContext)
{
SharedPreferences localSettings = mContext.getSharedPreferences("FileName",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localSettings.edit();
editor.putString("Url", info.Url);
editor.putString("Email", info.Email);
}
}
回答by Raghunandan
You need to pass the context to the constructor of non activity class
您需要将上下文传递给非活动类的构造函数
ItemsStore itemstore = new ItemStore(myActivity.this);
itemstore.SetItems(Info);
Then
然后
Context mContext;
public ItemsStore (Context context)
{
mContext =context;
}
Now mContext
can be used as Activity Context.
现在mContext
可以用作活动上下文。
Note: Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
注意:不要保留对上下文活动的长期引用(对活动的引用应该与活动本身具有相同的生命周期)
回答by Sushil
Write a public function in your activity. While creating an instance of your helper class in Activity class, pass the context of activity in constructor.
在您的活动中编写一个公共函数。在 Activity 类中创建助手类的实例时,在构造函数中传递活动的上下文。
Then from your helper class, using the activity context, call the public function in activity class.
然后从您的助手类,使用活动上下文,调用活动类中的公共函数。
回答by Gaskoin
Instead of creating memory leaks (by holding activity context in a class field) you can try this solution because shared preferences do not need activity context but ... any context :) For long living objects you should use ApplicationContext.
您可以尝试此解决方案,而不是创建内存泄漏(通过在类字段中保存活动上下文),因为共享首选项不需要活动上下文,但......任何上下文:) 对于长期存在的对象,您应该使用 ApplicationContext。
Create the application class:
创建应用程序类:
public class MySuperAppApplication extends Application {
private static Application instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static Context getContext() {
return instance.getApplicationContext();
}
}
Register it at manifest
在清单中注册
<application
...
android:name=".MySuperAppApplication" >
...
</application>
Then you can do something like this
然后你可以做这样的事情
public void persistItems(Information info) {
Context context = MySuperAppApplication.getContext();
SharedPreferences sharedPreferences = context.getSharedPreferences("urlPersistencePreferences", Context.MODE_PRIVATE);
sharedPreferences.edit()
.putString("Url", info.Url)
.putString("Email", info.Email);
}
Method signature looks better this way because it does not need external context. This can be hide under some interface. You can also use it easily for dependency injection.
方法签名以这种方式看起来更好,因为它不需要外部上下文。这可以隐藏在某些界面下。您还可以轻松地将它用于依赖项注入。
HTH
HTH