Comments (12)
I had this issue too. A way around this is to save the View that gets inflated as in field of your Fragment class. Then in your fragment's onCreateView() method you can check if your View field is null - if it is, inflate it, else return your already inflated View. Similarly, for data load, only load data if your View field is null, else do nothing.
from fragnav.
@codeteo Fragment.onCreate() returns a View. When you inflate your View, save it off as a field of your fragment class. So something like this:
public class MyFragment extends Fragment {
private View cachedView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (cachedView == null) {
// Inflate and populate
cachedView = inflater.inflate(R.layout.fragment_layout, container, false);
}
return cachedView;
}
}
from fragnav.
@codeteo Yes, different then the fragment's state. http://daniel-codes.blogspot.com/2012/06/fragment-transactions-reference.html here's a quick read up on the difference between show/hide and detach/reattach. What is suggested above is to save a field variable of the View, so that you don't have to reinflate the view. When the view is detached, it's view is destroyed, but all of its field variables are maintained. The problem with using "Hide" on a bunch of views is that it can be fairly taxing on memory (why it destroys the view on detach instead of maintaining it) so the developer has to be careful. That said, it shouldn't be up to the library to determine that, and should be up to the developer to decide to detach vs hide.
from fragnav.
Same issue..
from fragnav.
Use the answer from @two1stnamz, or just cache data, and not the entire view.
The idea from the Material Design spec is that the view should reset state.
Caching data and/or some of the state might be a good idea for performance reasons.
from fragnav.
Can you elaborate I bit more on : A way around this is to save the View that gets inflated as in field of your Fragment class. ??
Is it different from saving/restoring fragment's state ?
from fragnav.
Considering adding this as an enhancement moving forward. Reasonably quick to add, but shown/hidden state isn't maintained and would need to be added to the savedInstanceState. If anyone wants to make a PR for this, it would be helpful.
from fragnav.
I agree @ncapdevi. It is a pain to debug memory issues - and this library shouldn't be the cause of any :)
from fragnav.
@hauthorn @two1stnamz Any suggestions on how this should be implemented. It can either be
- A global final setting defined in the constructor
- A setting that can be toggled via a setter
- A parameter used in every function call. (e.g,
push(boolean bDetach)
)
Not sure what the most common use case is here.
from fragnav.
I don't think you should change your code to accomodate this, @ncapdevi.
The extra setting might confuse future developers, and it promotes heavy memory usage (which might trigger an OOM error for the developer later on).
from fragnav.
@hauthorn Ah! I thought you were requesting it be added as well. Yeah, that's my current thinking as well. Allowing it encourages bad behavior without devs fully realizing what's happening. I'm going to continue to leave it out for now unless a strong case is put forth to add it. Thanks for the feedback.
from fragnav.
For example, SharedElements ReturnTransition does not work for this reason, but there are so many other situations where i'd like to keep previous fragments attacched to my activity.
More than othes, i'd like to have a method param like this
pushFragment(Fragment frag, boolean keepAttached) or similar.
from fragnav.
Related Issues (20)
- FragNav - Without tabbar HOT 2
- Clear stack fail with fragmentHideStrategy = DETACH_ON_NAVIGATE_HIDE_ON_SWITCH
- How to send argument when switching tab
- What is the best practice show full screen fragment without independent from stack HOT 1
- Code Repetition, Unnecessary Assignment of Variable
- "Unable to instantiate fragment" when restore app from background after a long time HOT 2
- Migration from JCenter HOT 16
- When the current fragment is removed from the stack (fragNavController.popFragment()), the previous fragment is recreated
- How can i push fragment like iOS modal presentation ?
- Sending Parcellables
- FragNavController in Java
- Hide Strategy HOT 1
- How to use Shared Elements Transition with FragNav ? HOT 2
- Memory Leak when the user closes the dialog shown via FragNavController.showDialogFragment
- Implement Deeplink with FragNav
- Cannot find or not found code in Kotlin dsl HOT 1
- recreating activity after screen rotation
- pushFragment PreferenceFragmentCompat
- Failed to resolve: com.ncapdevi:frag-nav:3.2.0 jcenter error HOT 1
- fragments in viewpager not loading on return
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fragnav.