fixed app for non nfc devices & fixed navigation after app pause & resume (it seems...
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / NavigationManager.java
index 33d2f1c541938de315e4281b30e7bb31fb07e013..4a168a2212c829cedeae047bc5f2c505e9ebf528 100644 (file)
@@ -12,6 +12,7 @@ import android.widget.PopupWindow;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.List;
 
 import de.dhbwloe.campusapp.fragments.PopupFragment;
 
@@ -50,6 +51,13 @@ public class NavigationManager {
     public String getCurrentPageName() {
         if(oCurrentPage == null)
             return null;
+        if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
+            if(oCurrentFragment.isAdded())
+                return oCurrentPage.name;
+            else {
+                closeDialog();
+            }
+        }
         return oCurrentPage.name;
     }
 
@@ -101,9 +109,16 @@ public class NavigationManager {
             oParentPage = oCurrentPage;
             oParentFragment = oCurrentFragment;
             history = false;
-        } else if (oCurrentPage != null) {
-            transaction.replace(iFragmentContainerId, fragment);
         } else {
+            // remove fragments
+            List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
+            if(al != null) {
+                for (Fragment frag : al) {
+                    if (frag != null && frag.isAdded()) {
+                        transaction.remove(frag);
+                    }
+                }
+            }
             transaction.add(iFragmentContainerId, fragment);
         }
         if (history)
@@ -160,18 +175,22 @@ public class NavigationManager {
     public boolean closeDialog() {
         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
             PopupFragment fragment = (PopupFragment) oCurrentFragment;
-            fragment.destroyView();
+            boolean wasAdded = false;
+            if(oCurrentFragment.isAdded()) {
+                fragment.destroyView();
 
-            FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
-            FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
+                FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
+                FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
 
-            transaction.remove(oCurrentFragment);
+                transaction.remove(oCurrentFragment);
+                transaction.commit();
+                wasAdded = true;
+            }
 
             oCurrentPage = oParentPage;
             oCurrentFragment = oParentFragment;
 
-            transaction.commit();
-            return true;
+            return wasAdded;
         }
         return false;
     }