How do I preload a snippet before showing it?

In my work, I have several full screen snippets, each one downloading some data from the internet (using an async task) and showing it to the user. Shards are shown one at a time.

To be more specific, each of the snippets reads some urls from the sqlite database and fetches the content before showing them in the list, if it matters. Data loading tasks can be performed in the OnCreate () function.

I would like to preload the entire snippet (at least start downloading) when I show the splash screen. Much like a viewer that preloads fragments of it.

I am wondering how to achieve this? I tried to initialize / create all fragments in the OnCreate () function of my activity, hoping that the OnCreate () of the fragments could be called earlier, but the OnCreate () and OnCreateView () functions for the fragments are not called until the fragment is so show the user.

+3


source to share


5 answers


It looks like you need to decouple your model (the data that is being loaded) from your view (the fragments). One way to do this is to start loading AsyncTasks in your activity rather than running them in every chunk. Then, when the slices are eventually rendered, they can display the loaded data (or a counter or other indication that the loading process is still running).



+1


source


The onActivityCreated (Bundle) Fragment tells the Fragment that its Activity has completed its own Activity.onCreate ().

So, your solution to this problem is initializing or creating or processing your stuff that you want to preload before creating the fragments, inside Fragment

onActivityCreated(Bundle)



see docs for lifecyle snippet

0


source


The earliest temp you can start loading, either in static singleplayer mode or in the application class

0


source


As a result, I do the following: (1) add all fragments to the container. This way they (and their view) will be created and initialized. (2) hide the ones that are not in use and only show the one that I would like to see. (3) use FragmentTrasaction.show () / FragmentTrasaction.hide () to control visibility instead of FragmentTrasaction.add () or FragmentTrasaction.replace ().

If you follow this approach, be warned that all chunks will be cached in memory. But the advantage is that the transition between the fragment will be fast and efficient.

0


source


I faced the same problem and then I used this method, suppose we have EditText in Fragment then we can use codes like this

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    //this method allows you to input or instantiate fragments before showing this to an activity conidering id is "editTextEditProfileFirstName"
    EditText firstName = (EditText) getActivity().findViewById(R.id.editTextEditProfileFirstName);

    firstName.setText("This is my first name", TextView.BufferType.EDITABLE);
    super.onViewCreated(view, savedInstanceState);
}

      

0


source







All Articles