2
I’m coming across this warning from Android Studio. Warning:(40, 5) Do not place Android context classes in Static Fields; this is a memory Leak (and also breaks Instant Run) 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 selecting the date I pass this information to an Edittext that is in the class that called this Datepicker Fragment with a . setText() but for this to work Edittext needs to be Static and why Android Studio keeps 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 still don’t understand 100% how language works, so I ask... You have a problem leaving it the way it is?
It seems to me that there is no problem, the value of the variable is needed only while the user is in Activity because once he clicks save the data will be saved in the database and the value of the variable will no longer matter. As I understand this String will be in memory, then it will be replaced by the new value when the user registers another customer. So the memory occupied by 1 static variable would be insignificant... would that be it? Or the data that goes through this variable overlap in memory?
The
DatePickerFragment
is being used for something? Why not use directly theDatePickerDialog
?– Leonardo Lima
How and where you declare
etAniver
?– Victor Stafusa
Victor, etAniver is outside the Oncreate in the main Activity, which calls the Datepicker.
– user76140
Leonardo, I don’t know, I searched a lot until I found this functional hahahaha
– user76140
This class returns a Datepickerdialog, but has how to create this Datepickerdialog within the main Activity?
– user76140
Victor, etAniver is declared this way:
public static EditText etAniver;
– user76140
Victor, the link between the variable and Edittext is made inside Oncreate in this way:
etAniver = (EditText) findViewById(R.id.et_aniver);
– user76140
Leonardo, I tried using the direct Datepickerdialog but my project is for API 19 and to use the Ondatesetlistener I need API 24 above
– user76140
@Pablohenriquecorrea Do not put the solution to the problem in the answer. [en.so] is a question and answer system, so the answer should be in the answer area. There is no problem answering the question itself, so use the "Post your answer" button at the bottom of the page. It is also unnecessary to add "Solved" in the heading Just mark an answer as accepted you are already indicating that the problem has been solved.
– Woss
Thanks Anderson, this is my first post.
– user76140
To solve this problem, just follow that answer here.
– Nakamoto