I'm currently developing an android app and I've encountered a trouble that I'm not able to resolve myself.
Introduction
I've created a new project and picked "Tabbed Activity" as a template for the project. As you may know, the project created with this template, has 2 .xml files: activity_main.xml (that contains AppBarLayout and ViewPager2) and fragment_main.xml. It also has MainActivity.kt set up to make tabs work, and ui.main package with 3 .kt files in it that are responsible for displaying tabs content e.g. "Fragment #1" etc.
What do I want to have
I need the application to have top action bar with title, logo and tabs navigation. In total, I need to have 3 different tabs (fragments) with their own layout and logic.
What did I do and what happend
So, I've customized the activity_main.xml layout, then created a new layout fragment_dashboard.xml for the one of the fragments that I want to have in the application.
I've deleted auto generated code and wrote my own. Since I'm mostly like as a beginner in android development, I've used google to learn how to bind tabs, fragments and main activity together. I found several articles that I considered suitable for me.
After I finished the code, I wanted to check how my customized action bar with tabs and the half-finished fragment_dashboard.xml layout look together.
So I tried to run the app and encoutered the problem: when app starts in the emulated phone there is just a white screen and nothing else... (before deleting the auto-generated code for tabs it run without any problems)
What I tried to do
First of all I tried to debug MainActivity.kt. I put a breakpoint at the first line of the function onCreate():
super.onCreate(savedInstanceState, persistentState)
But when I run app in debug mode, debuger does not stop at this breakpoint. Thus, I came to a conclusion: execution does not even get to the function onCreate().
So, the question is: what am I doing wrong and how can I fix it to be able to see tabs and their fragments?
Code
DashboardFragment.kt
class DashboardFragment() : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_dashboard, container, false);
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Log.i("DashboardFragment","onViewCreated")
super.onViewCreated(view, savedInstanceState)
}
}
ViewPagerFragmentStateAdapter.kt
class ViewPagerFragmentStateAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
var positionToPageName = mapOf(
0 to "Dashboard"
)
private var _pageNameToFragment = mapOf<String, Fragment>(
"Dashboard" to DashboardFragment()
)
override fun getItemCount(): Int = _pageNameToFragment.size
override fun createFragment(position: Int): Fragment {
val pageName = positionToPageName[position]
val page = _pageNameToFragment[pageName]
return page ?: DashboardFragment() as Fragment
}
}
ViewPagerFragmentStateAdapter.kt
class MainActivity : AppCompatActivity() {
private lateinit var tabLayout: TabLayout
private lateinit var viewPager: ViewPager2
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_main)
val adapter = ViewPagerFragmentStateAdapter(this)
viewPager = findViewById(R.id.view_pager)
viewPager.adapter = adapter
tabLayout = findViewById(R.id.tabs)
TabLayoutMediator(tabLayout, viewPager) {
tab, position -> tab.text = adapter.positionToPageName[position]
}.attach()
}
}
I've tried to change some things in the MainActivity.kt. It seems that the problem was just in the definition of the onCreate() function:
I've created a test project with "Tabbed Activity" template again and noticed that in the new project the persistentState argument is missing. So, I just removed it from definition in my project and the project started working well.
Now the definition of the onCreate() looks like this: