-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from samosfator/memory
Significant reduce memory consumption
- Loading branch information
Showing
20 changed files
with
316 additions
and
186 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 48 additions & 21 deletions
69
app/src/main/java/ua/samosfator/moduleok/FragmentUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
package ua.samosfator.moduleok; | ||
|
||
import ua.samosfator.moduleok.fragment.LoginFragment; | ||
import ua.samosfator.moduleok.fragment.LogoutFragment; | ||
import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; | ||
import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; | ||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentTransaction; | ||
import android.util.Log; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FragmentUtils { | ||
|
||
private static LoginFragment loginFragment; | ||
private static LogoutFragment logoutFragment; | ||
private static LastTotalFragment lastTotalFragment; | ||
private static ModulesFragment modulesFragment; | ||
private static List<Fragment> addedFragments = new ArrayList<>(); | ||
private static String TAG = "FragmentUtils"; | ||
|
||
public static void showFragment(FragmentTransaction fragmentTransaction, Fragment fragment) { | ||
|
||
hideFragments(fragmentTransaction); | ||
|
||
if (fragment.isAdded()) { | ||
Log.d(TAG, "showing... " + fragment.getClass().getSimpleName()); | ||
|
||
fragmentTransaction.show(fragment); | ||
} else { | ||
Log.d(TAG, "adding... " + fragment.getClass().getSimpleName()); | ||
|
||
removeOldSameFragments(fragmentTransaction, fragment); | ||
|
||
fragmentTransaction.add(R.id.main_container, fragment); | ||
fragmentTransaction.show(fragment); | ||
|
||
public static LoginFragment getLoginFragment() { | ||
if (loginFragment == null) loginFragment = new LoginFragment(); | ||
return loginFragment; | ||
addedFragments.add(fragment); | ||
} | ||
fragmentTransaction.commit(); | ||
} | ||
|
||
public static LogoutFragment getLogoutFragment() { | ||
if (logoutFragment == null) logoutFragment = new LogoutFragment(); | ||
return logoutFragment; | ||
private static void hideFragments(FragmentTransaction fragmentTransaction) { | ||
for (Fragment addedFragment : addedFragments) { | ||
fragmentTransaction.hide(addedFragment); | ||
} | ||
} | ||
|
||
public static LastTotalFragment getLastTotalFragment() { | ||
if (lastTotalFragment == null) lastTotalFragment = new LastTotalFragment(); | ||
return lastTotalFragment; | ||
private static void removeOldSameFragments(FragmentTransaction fragmentTransaction, Fragment targetFragment) { | ||
for (Fragment addedFragment : addedFragments) { | ||
if (addedFragment.getClass().getSimpleName().equals(targetFragment.getClass().getSimpleName())) { | ||
Log.d(TAG, "removing old same... " + addedFragment.getClass().getSimpleName()); | ||
fragmentTransaction.remove(addedFragment); | ||
} | ||
} | ||
} | ||
|
||
public static ModulesFragment getModulesFragment() { | ||
if (modulesFragment == null) modulesFragment = new ModulesFragment(); | ||
return modulesFragment; | ||
public static void removeFragment(FragmentManager fragmentManager, Fragment fragment) { | ||
Log.d(TAG, "removing... " + fragment.getClass().getSimpleName()); | ||
new Handler(Looper.getMainLooper()).post(() -> { | ||
fragmentManager.beginTransaction().remove(fragment).hide(fragment).commit(); | ||
fragmentManager.executePendingTransactions(); | ||
}); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/ua/samosfator/moduleok/FragmentsKeeper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package ua.samosfator.moduleok; | ||
|
||
import android.support.v4.app.Fragment; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import ua.samosfator.moduleok.fragment.LoginFragment; | ||
import ua.samosfator.moduleok.fragment.last_total_fragment.LastTotalFragment; | ||
import ua.samosfator.moduleok.fragment.modules_fragment.ModulesFragment; | ||
|
||
public class FragmentsKeeper { | ||
|
||
private static List<Fragment> all = Collections.synchronizedList(new ArrayList<>()); | ||
|
||
private static Fragment lastTotal = new LastTotalFragment(); | ||
private static Fragment modules = new ModulesFragment(); | ||
private static LoginFragment login = new LoginFragment(); | ||
|
||
public static Fragment getLastTotal() { | ||
return lastTotal; | ||
} | ||
|
||
public static void setLastTotal(Fragment lastTotal) { | ||
FragmentsKeeper.lastTotal = lastTotal; | ||
} | ||
|
||
public static Fragment getModules() { | ||
return modules; | ||
} | ||
|
||
public static void setModules(Fragment modules) { | ||
FragmentsKeeper.modules = modules; | ||
} | ||
|
||
public static LoginFragment getLogin() { | ||
return login; | ||
} | ||
|
||
public static void setLogin(LoginFragment login) { | ||
FragmentsKeeper.login = login; | ||
} | ||
|
||
public static List<Fragment> getAll() { | ||
populateFragmentsList(); | ||
return all; | ||
} | ||
|
||
private static void populateFragmentsList() { | ||
all.clear(); | ||
all.add(lastTotal); | ||
all.add(modules); | ||
all.add(login); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.