I'm running into this warning from Android Studio. Warning: (40, 5) Do not place Android context classes in static fields; this is a memory leak (and also Instant Run breaks) I have a button that opens a DatePicker fragment to select the date.
// Botão para chamar o DatePicker Fragment na Activity principal
btnSelecionaData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
});
When I select the date I pass this information to an EditText that stays in the class that called that DatePicker Fragment with a .setText () but for this to work EditText needs to be static and so Android Studio is giving this warning.
// Classe DatePickerFragment
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
//######### ESTE É O EDITTEXT STATIC DA ACTIVITY PRINCIPAL, O QUAL EU PRECISO SETAR COM A DATA ASSIM QUE O USUÁRIO CLICAR NO OK DO FRAGMENT
etAniver.setText(day+"/"+(month+1)+"/"+year);
//#########
}
}
I do not yet understand 100% language work, so I ask ... Is it okay to leave it as it is?
For me it seems like no problem, the value of the variable is only needed while the user is in the activity because as soon as he clicks save the data will be saved in the database and the value of the variable will not matter anymore. To my understanding this String will stay in memory, then it will be replaced by the new value when the user registers another client. So the memory occupied by 1 static variable would be insignificant ... would it? Or does the data that passes through this variable overlap in memory?