I would like to create an easy todo appliacation, that stores data in shared preferences. Therefore I have created a class called PrefManager, which one contains all of my methods in connection managing preferences. In this case the method "getSharedPreferences" needs a context. Thats okay, but I have to use this class all in my other classes, included the recyclerView adapter class.
So i made this "solution" (ToDoActivity):
public static PrefManager mPrefManager;
private RecyclerView mRecyclerView;
private ToDoRecyclerViewAdapter adapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
context = getApplicationContext();
adapter = new ToDoRecyclerViewAdapter(this);
mPrefManager = new PrefManager(context, adapter);
mPrefManager.setupThePreferences();
...
and imported everywhere:
import static bla.bla.bla.ToDoActivity.mPrefManager;
But I get this error:
Do not place Android context classes in static fields (static reference to PrefManager which has field context pointing to Context); this is a memory leak (and also breaks Instant Run). A static field will leak contexts.
How can I make it better? How can I solve this?
EDIT
I use it for example in the RecycleView Adapter by deleting an item:
public class ToDoRecyclerViewAdapter extends RecyclerView.Adapter <ToDoRecyclerViewAdapter.ViewHolder>
There:
holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int identifier = holder.myTodo.getIdentifier();
removeTodo(position);
mPrefManager.deleteThePref(identifier);
return false;
}
});
Or in my Todo creating activity:
public class CreateActivity extends Activity
There:
btnCreateExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPrefManager.saveNewToDo(editTodoTitle.getText().toString(), spinnerTodoPriority.getSelectedItemPosition(), editTodoDescription.getText().toString(), date);
finish();
}
});
In TodoActivity
,declare PrefManager and generate getter
for it.
private PrefManager mPrefManager; public PrefManager getmPrefManager{ return mPrefManager; }
In Adapter
,when you delete an item:
removeTodo(position); ((TodoActivity)context).getmPreManager().deleteThePref(identifier);
Recreate constructor for PrefManager
and pass only context
:
public PrefManager(Context context){ super(); context.getSharedPreferences(...); }
In CreateActivity
, new a PrefManager
:
mPrefManager = new PrefManager(context); mPrefManager.setupThePreferences();
Try extending your activities to access your ToDoActivity.
For example:
public class AnotherActivity extends ToDoActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do);
context = getApplicationContext();
adapter = new ToDoRecyclerViewAdapter(this);
mPrefManager = new PrefManager(context, adapter);
mPrefManager.setupThePreferences();
...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.