From 3812f6ccf9d5796f25f81aeea942c25b58296d01 Mon Sep 17 00:00:00 2001 From: marcinzelent Date: Wed, 25 Apr 2018 11:25:27 +0200 Subject: Fixed crash on reload of observations view --- .../liberavem/AllObservationsFragment.java | 2 +- .../org/marcinzelent/liberavem/DataKeeper.java | 33 +++++++++++++++------- .../org/marcinzelent/liberavem/MainActivity.java | 4 --- .../liberavem/MyObservationsFragment.java | 2 ++ .../liberavem/ObservationsFragment.java | 2 ++ 5 files changed, 28 insertions(+), 15 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java index af4331a..b54227c 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java @@ -39,7 +39,6 @@ public class AllObservationsFragment extends Fragment { // Required empty public constructor } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.observations_list, container, false); @@ -60,6 +59,7 @@ public class AllObservationsFragment extends Fragment { } ); + DataKeeper.getInstance().addFragment(this); DataKeeper.getInstance().downloadData(getActivity()); } diff --git a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java index 2000eab..5fa975b 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java +++ b/app/src/main/java/org/marcinzelent/liberavem/DataKeeper.java @@ -1,6 +1,7 @@ package org.marcinzelent.liberavem; import android.app.Activity; +import android.app.Fragment; import android.widget.Toast; import com.android.volley.Request; @@ -28,6 +29,7 @@ public class DataKeeper { private DataKeeper() { } + private List fragments = new ArrayList(); private Bird[] birds; private Observation[] observations; @@ -98,18 +100,29 @@ public class DataKeeper { Volley.newRequestQueue(activity).add(observationsRequest); } + public void addFragment(Object fragment) { + fragments.add(fragment); + } + + public void clearFragments() { + fragments.clear(); + } + private void callPopulator(Activity activity) { if (birds != null && observations != null) { - MyObservationsFragment mo = (MyObservationsFragment) ((MainActivity)activity).getObservationFragments().get(0); - List myObservationsList = new ArrayList<>(); - for (Observation o : observations) - if (o.getUserId().equals("Sminem")) myObservationsList.add(o); - - Observation[] myObservations = new Observation[myObservationsList.size()]; - myObservations = myObservationsList.toArray(myObservations); - mo.populateList(myObservations, birds); - AllObservationsFragment aof = (AllObservationsFragment) ((MainActivity)activity).getObservationFragments().get(1); - aof.populateList(observations, birds); + for(Object fragment : fragments) { + if (fragment.getClass() == AllObservationsFragment.class) + ((AllObservationsFragment) fragment).populateList(observations, birds); + else { + List myObservationsList = new ArrayList<>(); + for (Observation o : observations) + if (o.getUserId().equals("Sminem")) myObservationsList.add(o); + + Observation[] myObservations = new Observation[myObservationsList.size()]; + myObservations = myObservationsList.toArray(myObservations); + ((MyObservationsFragment) fragment).populateList(myObservations, birds); + } + } } } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java index 4009f8c..b32358a 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java @@ -139,8 +139,4 @@ public class MainActivity extends AppCompatActivity public void onFragmentInteraction(Uri uri) { } - - public List getObservationFragments() { - return observationsFragment.getChildFragmentManager().getFragments(); - } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java index b83d7b4..c3656e9 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java @@ -46,6 +46,8 @@ public class MyObservationsFragment extends Fragment { } } ); + + DataKeeper.getInstance().addFragment(this); } public void populateList(final Observation[] observations, final Bird[] birds) { diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java index 128cbe9..e2df607 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java @@ -64,6 +64,8 @@ public class ObservationsFragment extends Fragment { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } + + DataKeeper.getInstance().clearFragments(); } @Override -- cgit v1.2.3