diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index a1044b1..e7d13a4 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -40,6 +40,8 @@ import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.views.CustomInterceptor; import pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout; +import java.util.List; + public class MainActivity extends ActionBarActivity { private static final Logger lg = LoggerFactory.getLogger(MainActivity.class); @@ -245,11 +247,11 @@ public class MainActivity extends ActionBarActivity { } - private void switchFragment(Fragment fragment, String tag) { + private void switchFragment(Fragment newFragment, String tag) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ctx = fragmentManager.beginTransaction(); - lg.trace("old fragment id: {}, current fragment id: {}", oldFragmentId, currentFragmentId); + lg.trace("old newFragment id: {}, current newFragment id: {}", oldFragmentId, currentFragmentId); if (oldFragmentId != currentFragmentId) { if (currentFragmentId < oldFragmentId) { // slide left animation @@ -270,11 +272,28 @@ public class MainActivity extends ActionBarActivity { } } - Fragment frag = fragmentManager.findFragmentByTag(tag); - if (frag == null) { - ctx.replace(R.id.fragments, fragment, tag); + final List fragments = fragmentManager.getFragments(); + if (fragments != null) { + lg.trace("Available fragments {}", fragments.size()); + for (Fragment frag : fragments) { + if (frag != null) { + lg.trace("Available fragment with id: {}, tag: {}", frag.getId(), frag.getTag()); + } else { + lg.trace("Available null-fragment"); + } + } } else { - ctx.replace(R.id.fragments, frag); + lg.trace("There are no fragments -> null"); + } + + lg.trace("Trying to search newFragment by tag {}", tag); + Fragment currFragment = fragmentManager.findFragmentByTag(tag); + if (currFragment == null) { + lg.trace("Using new newFragment: {}", System.identityHashCode(newFragment)); + ctx.replace(R.id.fragments, newFragment, tag); + } else { + lg.trace("Reusing old newFragment: {}", System.identityHashCode(currFragment)); + ctx.replace(R.id.fragments, currFragment); } // ctx.addToBackStack(MAIN_BACKSTACK); ctx.commit(); @@ -429,4 +448,17 @@ public class MainActivity extends ActionBarActivity { lg.trace("2 Saving current fragment id: {}", currentFragmentId); } + @Override + protected void onStop() { + super.onStop(); + + lg.trace("onStop {}", System.identityHashCode(this)); + } + + @Override + protected void onStart() { + super.onStart(); + + lg.trace("onStart {}", System.identityHashCode(this)); + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index 96c73f3..4d3e86f 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -66,7 +66,7 @@ public class HomeFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - lg.trace("onCreate"); + lg.trace("onCreate {}", System.identityHashCode(this)); pageId = 1L; loading = new Semaphore(1, true); noMorePhotos = false; @@ -249,31 +249,39 @@ public class HomeFragment extends Fragment { new PanoramioResponseCallback() { @Override public void callback(PanoramioResponseStatus status, List images, Long imagesCount) { - lg.debug("Fetched with status: {}, images: {}, count: {}", status, images, imagesCount); - if (status != PanoramioResponseStatus.SUCCESS) { - return; + try { + lg.debug("Fetched with status: {}, images: {}, count: {}", status, images, imagesCount); + if (status != PanoramioResponseStatus.SUCCESS) { + return; + } + + ListView locations = (ListView) getView().findViewById(R.id.locations); + if (locations == null) { + lg.trace("Empty locations"); + return; + } + ArrayAdapter adapter = (ArrayAdapter) locations.getAdapter(); + photos.addAll(images); + if (photos.isEmpty()) { + Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); + } + noMorePhotos = images.isEmpty(); + if (adapter == null) { + locations.setAdapter(new PanoramioAdapter(activity, R.id.list_item, images)); + } else { + adapter.addAll(images); + } + + // TODO we can think about removing first items also and last if the number + // TODO of items exceeds the limit (to save the memory) + + lg.debug("Finished Fetching additional photos count: {}", photos.size()); + + } finally { + lg.trace("Releasing fetching lock"); + loading.release(); } - ListView locations = (ListView) getView().findViewById(R.id.locations); - ArrayAdapter adapter = (ArrayAdapter) locations.getAdapter(); - photos.addAll(images); - if (photos.isEmpty()) { - Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); - } - noMorePhotos = images.isEmpty(); - if (adapter == null) { - locations.setAdapter(new PanoramioAdapter(activity, R.id.list_item, images)); - } else { - adapter.addAll(images); - } - - // TODO we can think about removing first items also and last if the number - // TODO of items exceeds the limit (to save the memory) - - lg.debug("Finished Fetching additional photos count: {}", photos.size()); - - loading.release(); - } } @@ -411,4 +419,17 @@ public class HomeFragment extends Fragment { lg.trace("Saved photos: {}", photos); } + @Override + public void onStop() { + super.onStop(); + + lg.trace("onStop {}", System.identityHashCode(this)); + } + + @Override + public void onStart() { + super.onStart(); + + lg.trace("onStart {}", System.identityHashCode(this)); + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index 5b7e2d3..f3a72ee 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -56,7 +56,7 @@ public class WikiLocationsFragment extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - lg.trace("onCreate"); + lg.trace("onCreate {}", System.identityHashCode(this)); appObjects = savedInstanceState == null ? new ArrayList() : (ArrayList)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);; } @@ -99,6 +99,12 @@ public class WikiLocationsFragment extends Fragment { public void fetchWikiLocations() { lg.trace("Fetch wiki locations"); + final FragmentActivity activity = getActivity(); + if (activity == null) { + lg.warn("Activity shouldn't be null. No headless fragment"); + return; + } + MainActivity mainActivity = (MainActivity) getActivity(); if (lastFetchSize == 0) { @@ -113,12 +119,6 @@ public class WikiLocationsFragment extends Fragment { return; } - final FragmentActivity activity = getActivity(); - if (activity == null) { - lg.warn("Activity shouldn't be null. No headless fragment"); - mainActivity.hideProgress(); - return; - } final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); if (location == null) { @@ -192,6 +192,7 @@ public class WikiLocationsFragment extends Fragment { getActivity().setTitle("Wiki search"); updateLocationInfo(); fetchWikiLocations(); + lg.trace("onResume {}", System.identityHashCode(this)); } public void updateLocationInfo() { @@ -212,6 +213,7 @@ public class WikiLocationsFragment extends Fragment { @Override public void onPause() { super.onPause(); + lg.trace("onPause {}", System.identityHashCode(this)); } @Override @@ -221,4 +223,10 @@ public class WikiLocationsFragment extends Fragment { outState.putSerializable(WIKI_APP_OBJECTS, appObjects); } + + @Override + public void onDestroy() { + super.onDestroy(); + lg.trace("onDestroy {}", System.identityHashCode(this)); + } }