Deepan
Deepan

Reputation: 853

App slows down when more than 20 fragments are added to backstack

I have a fragment that loads tabs dynamically into the view pager where data in each tab will be loaded on the result of an api call. Each fragment will have a minimum of 4 tabs. When this fragment is added into the backstack multiple times (normally more than 20), the app starts to lag and it becomes very slow to scroll through the list. Is there a way to solve this?

Upvotes: 0

Views: 260

Answers (1)

Khemraj Sharma
Khemraj Sharma

Reputation: 58974

This issue is related to managing UI Thread & Worker Thread.

Perhaps you are doing so much work in UI Thread. Like filtering list or creating Map (It can be anything, as I cant see your code.) etc.

Solution:

You should do only UI work in UI or Main thread. Like if you are fetching & parsing response.

  • Fetch api data in Worker Thread (AsyncTask() or new Handler()).
  • Parse data in worker thread, because parsing too much data will cause UI lagging.
  • Filter lists or prepare data in same worker thread.
  • Then use runOnUiThread(); to set your data in List or to set view components.
  • If you have long list in every fragment then use Pagination.

There can be many other improvements in your code. You should identify and resolve that.

Keypoint is use UI/Main thread for communicating to View. And use Worker Thread for background process.

If you properly manage threads in your app, your app will never hang.

Upvotes: 1

Related Questions