Asynctask and Fragments

Asked

Viewed 84 times

0

Hello, I’m new to development and have some questions.

I have to implement a new layout, where:

  1. I converted the activities that load the data into fragments;
  2. In a new Activity the graph fragment is at the top and the one that loads the list is below, dividing the screen.

Metaactivity: converted to Fragment, brings a Listview, when clicking on a record goes to Activity Fragmentosperiodosanterioresactivity

--FragmentosPeriodosAnterioresActivity

----(Fragment Top) Periodographactivity (converted to Fragment)

----(Fragment Bottom) Periodoactivity (converted to Fragment) loads the data into an Arraylist with the data, within the asynchronous method

The reason I converted them instead of creating a new class with fragment nomenclature is because Async does not perform the routine, it only works in this class...

The Periodoactivity class currently in the onActivityCreated method loads the goals in the webservice listview from Asynctask and stores them as a "global variable":

...
callBack = new AsyncCallBack() {
  public void onTesting(Object res) {
    ...
    Intent listaMetas = getActivity().getIntent();
    Bundle extras = new Bundle();
    extras.putSerializable("ListaPeriodo", metas);
    listaMetas.putExtras(extras);
  }
}

The Periodographactivity Class (Loads an Androidplot with the data generated in the Periodoactivity fragment and saved in the "Listperiodo" serializable) // in the onStart method()

Intent listaMetas = getActivity().getIntent(); 
ArrayList<Cenario1> teste = new ArrayList<Cenario1>();
metas =  (ArrayList<Cenario1>) getActivity().getIntent().getSerializable("ListaPeriodo"); // retorna nulo

-> My big doubt, it is possible to choose the time or when the asynchronous method is executed?

-> It is best practice to load asynchronous data into fragments?

Android Fragments and Activity lifecicle monitor log:

...
03-31 09:32:03.678 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosMetaActivity onCreateView (not implemented)
03-31 09:32:03.686 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosMetaActivity onCreateView (not implemented)
03-31 09:32:03.780 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosMetaActivity onCreateView (not implemented)
03-31 09:32:13.717 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosMetaActivity onPause (not implemented)
03-31 09:32:13.780 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosPeriodosAnterioresActivity onStart (not implemented)
03-31 09:32:13.780 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoGraphActivity onCreate 
03-31 09:32:13.780 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoGraphActivity onCreateView 
03-31 09:32:13.842 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosPeriodosAnterioresActivity onStart (not implemented)
03-31 09:32:13.842 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoActivity onCreate
03-31 09:32:13.842 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoActivity onCreateView
03-31 09:32:13.866 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosPeriodosAnterioresActivity onCreate
03-31 09:32:13.866 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoGraphActivity onActivityCreated
03-31 09:32:13.866 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoActivity onActivityCreated - (chamada para o metodo assíncrono)
03-31 09:32:13.951 25105-25105/com.amend.cockpit D/LifeCicle: FragmentosPeriodosAnterioresActivity onStart
03-31 09:32:13.951 25105-25174/com.amend.cockpit D/LifeCicle: AsyncBlock doInBackground
03-31 09:32:13.951 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoGraphActivity onStart - (Testativa de recuperar os dados aqui)
03-31 09:32:13.951 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoActivity onStart
03-31 09:32:14.373 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoActivity AsyncCallBack
03-31 09:32:14.412 25105-25105/com.amend.cockpit D/LifeCicle: PeriodoActivity list putExtras - (meu ArrayList é populado somente aqui)
  • Your question is hard to understand (at least for me). You can forget your app and put the doubt in a simpler scenario that can be played?

  • My asynchronous method always runs last before the fragments load, and my chart does not receive the data. It is my first contact with asynchronous methods, so my question is whether the execution of the asynchronous method of a fragment A can be delayed so that a fragment B can pick up the data, after the asynchronous method execute and populate the data for my Arraylist.

  • Why not place this method on fragment B?

  • 1

    At first there was another error "such instance field" when executing the asynchronous method in fragment B. But I put the routine inside the asynchronous method of fragment B, my Arraylist was populated, now my Androidplot has not yet drawn the graph that I will discover. I appreciate the support!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.