简体   繁体   中英

How can I save and restore fragment state in bottom navigation material design?

Other fragments exist within bottom navigation fragment and contains data from the server! The data seems to be there as it loads quickly than before but the view gets destroyed and loads again delaying the process.

Well, when you load the fragment you need to check if the data is already there before making the network request again.

Let's say you call the network on "onCreateView" method. You have a global variable in your class.

class YourClass{
var someVariable = 0
...
//here your call network on "onCreateView" method

someVariable = callingNetWork.getValueFromNetWork()

}

onCreateView is called from fragment lifecycle, you cannot control how many times it will be called. So you need to check if already have the data. You need a logic like this:

if( someVariable == 0) { //Has not been modiefied yet
someVariable = callingNetWork.getValueFromNetWork()
}
//Don't need an else, if it's not equal to 0, you have already called the networkd and stored the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM