I can not understand where it is better to call the method for filling the fragment with data from the Loader. Depending on the requested application mode of operation, a fragment is attached to the activity, which is filled with data from the corresponding Loader. Like that:

Cursor cursor; //Курсор который инициализируется лоадером getLoaderManager().initLoader(0, null, this); someFragment f = new SomeFragment(); if ( cursor.moveToFirst() ) { f.getData(cursor.getString(0)) //передает данные из курсора во внутренний метод фрагмента, который наполняет его информацией } getFragmentManager().beginTransaction().add(R.id.frame, f).commit(); //и заполненный фрагмент выводится на экран 

Since the loader loads the data in a separate thread, it is not time to initialize the cursor and in the line cursor.moveToFirst () the null object is accessed.

Is it possible to somehow suspend the output of the fragment before the cursor is initialized? I also tried to fill the fragment with data from the onLoadFinished () method; and output a fragment from there, but the note in the documentation confuses:

This is not the case.

It is not advisable to display an empty fragment on the screen before filling it with information. Can someone suggest how best to implement logic? I wanted to make something like an emerging progress bar that displays the progress of data loading and freezes the application interface (until the data and fragment appear, there is no UI, so this is not critical in this case), but I can't figure out how to pause the main thread at this point, determine the completion of the download and continue the work flow.

    1 answer 1

    use interfaces when your loader is loaded, call the interface method that will call someFragment f = new SomeFragment(); if ( cursor.moveToFirst() ) { f.getData(cursor.getString(0)) //передает данные из курсора во внутренний метод фрагмента, который наполняет его информацией } getFragmentManager().beginTransaction().add(R.id.frame, f).commit(); //и заполненный фрагмент выводится на экран someFragment f = new SomeFragment(); if ( cursor.moveToFirst() ) { f.getData(cursor.getString(0)) //передает данные из курсора во внутренний метод фрагмента, который наполняет его информацией } getFragmentManager().beginTransaction().add(R.id.frame, f).commit(); //и заполненный фрагмент выводится на экран

    and everything will be ok with you, everything you want

    • And you can a little more. What kind of interface and how you can call its method, if the interfaces have no implementation of methods. That is, someone must implement it, some class. Something I do not understand how to practically apply all this. - Evgeny Kuznetsov
    • 2
      @ YevgenyKuznetsov, as I understand it, he spoke about a Callback tyk - Silento
    • one
      Yes, that's right, this is Callback. - Valera Vovnov
    • one
      For an example here, the 1st answer see stackoverflow.com/questions/3398363/… - Valera Vovnov