In general, I have a fragment that displays currency rates for the selected date. I select the date using a DialogFragment with a DatePickerDialog inside.

 public void onClick(View view) { DialogFragment newFragment = new DatePickerFragment(); newFragment.setTargetFragment(HistoryCurrencyFragment.this, DatePickerFragment.REQUEST_CODE); newFragment.show(getFragmentManager(), "datePicker"); } 

then in the DatePickerFragment method in onDateSet I pass the selected date to my first fragment

 @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { HistoryCurrencyFragment.getInstance().setDate(year,monthOfYear,dayOfMonth); } 

I start this asynchronous field in the setDate method of the setDate class.

 public void setDate(int year, int monthOfYear, int dayOfMonth) { new HistoryCurrencyAsync(dayOfMonth + "." + monthOfYear + "." + year).execute(); } 

After I receive the data, I use a custom adapter to network into a sheet of views. The only problem is that after returning from DatePickerFragment to the fragment in which everything should be displayed, HistoryCurrencyFragment then the adapter and null leaves.

I initialized them in the onCreate method there specified setRetainInstance(true); so that the fragment would not be recreated. and tried to initialize all the same to null in the asynchronous onPostExecute method.

 @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); listAdapterHistory = new ListAdapterHistory(getActivity().getApplicationContext(), currencyLists); listView = new ListView(getContext()); listView.setAdapter(listAdapterHistory); } 

 @Override protected void onPostExecute(List<Currency> currencies) { super.onPostExecute(currencies); Log.e("Kурс валют за " + date + " size: " + currencies.size(), "onPostExecute"); currencyLists = currencies; Log.e("!!!!!!!!adapter!!!!!!!", "onPostExecute " + listAdapterHistory); Log.e("!!!!!!!!list!!!!!!", "onPostExecute" + currencyLists.size()); Log.e("!!!!!!!listview!!!!!!!", "onPostExecute" + listView); listAdapterHistory.notifyDataSetChanged(); } 

 E/!!!!!!!!adapter!!!!!!!: onPostExecute null E/!!!!!!!!list!!!!!!: onPostExecute20 E/!!!!!!!listview!!!!!!!: onPostExecutenull 

I can not understand what the problem is, please help

they become null then when I return from the DialogFragment to the first fragment.

  • one
    Here is HistoryCurrencyFragment.getInstance().setDate(year,monthOfYear,dayOfMonth); You create a new fragment and transfer values ​​to it. Therefore, it turns out that this fragment of the field has zero, since, generally speaking, it is not tied to activation. - post_zeew
  • Yes you are right. the mistake was exactly that. - drugs_and_code

1 answer 1

Here:

 HistoryCurrencyFragment.getInstance().setDate(year,monthOfYe‌​ar,dayOfMonth); 

You create a new fragment and transfer values ​​to it. Therefore, it turns out that this fragment of the field has zero, since, generally speaking, it is not tied to activation.