Reputation: 133
I used fragment to show Vehicle list in recycler view. Whenever user clicks on Vehicle then it shows its details in another fragment, but when i comeback on Vehicle list fragment after getting its details using back button pressed then Vehicle list fragment loads like first time. I used load more in recycler view. That's why whenever user scrolls much more and if he wanted to see details of the vehicle and then come back on list it reloads like first time and user gets first item back...
rvVehicleList.addOnScrollListener(new EndlessRecyclerViewScrollListener((LinearLayoutManager) rvVehicleList.getLayoutManager()) {
@Override
public void onLoadMore(int page, int totalItemsCount) {
if (!vehicleSearchResponseModel.getData().get_links().getNext().equals("")) {
pageNo++;
searchMoreVehicle(pageNo);
}
}
});
holder.llMainView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Bundle bundle = new Bundle();
bundle.putSerializable("vehicleInfo", vehicleSearchPagerList.get(position));
VehicleAdInfoFragment vehicleAdInfoFragment = new VehicleAdInfoFragment();
vehicleAdInfoFragment.setArguments(bundle);
fragmentTransaction.replace(R.id.frContainer, vehicleAdInfoFragment);
fragmentTransaction.addToBackStack(vehicleAdInfoFragment.getTag());
fragmentTransaction.commit();
}
});
Upvotes: 1
Views: 3823
Reputation: 8305
To restrict the recreation of the fragment, what I did:
In onCreateView
you can store inflated view in a global variable and initialize it only if it is null
, like in this code:
var root:View?=null
var apiDataReceived=false
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
if (root==null)
root=inflater!!.inflate(R.layout.fragment_layout, container, false)
return root
}
Now if you are parsing some data and fill it into RecyclerView
or any other View
Make a global variable like in the above code apiDataReceived
Set it to true if you successfully parsed data.
Before apiCalls place a condition like this:
if (!apiDataReceived) { apiCalls() }
So if apiCalls() would be called only if data is not parsed.
Do your HTTP calls and parsing or any other thing in a method which called after onCreateView
like onStart
The above code is in kotlin, If you are facing any issue, let me know in the comments.
Upvotes: 3
Reputation: 189
One solution could be where you do not need to replace fragment instead use add() method. This is the only thing that worked in my case to avoid reloading of previous fragment.
fun addFragment(fragment: Fragment)
{
val transaction = supportFragmentManager.beginTransaction()
val currentFragment = supportFragmentManager .findFragmentById(R.id.container) //get current fragment
transaction.hide(currentFragment!!) //hide current fragment
transaction.add(R.id.container, fragment) //add next fragment
transaction.addToBackStack(null)
transaction.commit()
}
Upvotes: -1
Reputation: 101
You can try something like this in your vehicle list fragment:
@Override
public void onPause(){
super.onPause();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = preferences.edit();
mCurrentIndex = ((LinearLayoutManager) rvVehicleList.getLayoutManager()).findFirstVisibleItemPosition();
editor.putInt("current_position", mCurrentIndex);
editor.apply();
}
@Override
public void onResume(){
super.onResume();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
mCurrentIndex = preferences.getInt("current_position", 0);
rvVehicleList.getLayoutManager().scrollToPosition(mCurrentIndex);
}
Upvotes: 0