Im working on an app, which has a Fragment containing a RecyclerView, which looks for saved songs data using a cursor. Whenever i open the fragment it takes some milliseconds to load. The Verbose infact flags the skipped frames:
Skipped 33 frames! The application may be doing too much work on its main thread.
What can i do to fix this? Where can i learn more about this?
Heres my fragment.kt code:
class FragmentTrack : Fragment() {
var trackList = mutableListOf<DataItems>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.fragment_track, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var songCursor : Cursor? = activity?.contentResolver?.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null, null, null, null)
while (songCursor != null && songCursor.moveToNext()) {
var songName = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
var songArtist = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
trackList.add(DataItems(songName, songArtist))
}
rvTracks.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(activity)
adapter = AdapterList(trackList)
}
topToolbar.inflateMenu(R.menu.menu_toolbar)
topToolbar.setNavigationOnClickListener {
startActivity(Intent(activity, ActivitySearch::class.java))
}
topToolbar.setOnMenuItemClickListener {
when(it.itemId) {
R.id.fsvSettings -> this.startActivity(Intent(activity, ActivitySettings::class.java))
}
true
}
}
}