Fix uninitialized fragment after restoring the activity (#237)

This commit is contained in:
Rafał Borcz 2019-02-12 00:41:02 +01:00 committed by Mikołaj Pich
parent 1b1f2ae3bb
commit 9fcf245ecd
5 changed files with 27 additions and 25 deletions

View File

@ -6,16 +6,14 @@ import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.RoomDatabase.JournalMode.TRUNCATE import androidx.room.RoomDatabase.JournalMode.TRUNCATE
import androidx.room.TypeConverters import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import io.github.wulkanowy.data.db.dao.AttendanceDao import io.github.wulkanowy.data.db.dao.AttendanceDao
import io.github.wulkanowy.data.db.dao.AttendanceSummaryDao import io.github.wulkanowy.data.db.dao.AttendanceSummaryDao
import io.github.wulkanowy.data.db.dao.ExamDao import io.github.wulkanowy.data.db.dao.ExamDao
import io.github.wulkanowy.data.db.dao.GradeDao import io.github.wulkanowy.data.db.dao.GradeDao
import io.github.wulkanowy.data.db.dao.GradeSummaryDao import io.github.wulkanowy.data.db.dao.GradeSummaryDao
import io.github.wulkanowy.data.db.dao.MessagesDao
import io.github.wulkanowy.data.db.dao.HomeworkDao import io.github.wulkanowy.data.db.dao.HomeworkDao
import io.github.wulkanowy.data.db.dao.LuckyNumberDao import io.github.wulkanowy.data.db.dao.LuckyNumberDao
import io.github.wulkanowy.data.db.dao.MessagesDao
import io.github.wulkanowy.data.db.dao.NoteDao import io.github.wulkanowy.data.db.dao.NoteDao
import io.github.wulkanowy.data.db.dao.SemesterDao import io.github.wulkanowy.data.db.dao.SemesterDao
import io.github.wulkanowy.data.db.dao.StudentDao import io.github.wulkanowy.data.db.dao.StudentDao
@ -26,9 +24,9 @@ import io.github.wulkanowy.data.db.entities.AttendanceSummary
import io.github.wulkanowy.data.db.entities.Exam import io.github.wulkanowy.data.db.entities.Exam
import io.github.wulkanowy.data.db.entities.Grade import io.github.wulkanowy.data.db.entities.Grade
import io.github.wulkanowy.data.db.entities.GradeSummary import io.github.wulkanowy.data.db.entities.GradeSummary
import io.github.wulkanowy.data.db.entities.Message
import io.github.wulkanowy.data.db.entities.Homework import io.github.wulkanowy.data.db.entities.Homework
import io.github.wulkanowy.data.db.entities.LuckyNumber import io.github.wulkanowy.data.db.entities.LuckyNumber
import io.github.wulkanowy.data.db.entities.Message
import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Note
import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student

View File

@ -1,6 +1,5 @@
package io.github.wulkanowy.ui.base package io.github.wulkanowy.ui.base
import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter import androidx.fragment.app.FragmentPagerAdapter
@ -9,9 +8,10 @@ class BaseFragmentPagerAdapter(private val fragmentManager: FragmentManager) : F
private val pages = mutableMapOf<Fragment, String?>() private val pages = mutableMapOf<Fragment, String?>()
private var containerId = 0 var containerId = 0
fun getFragmentInstance(position: Int): Fragment? { fun getFragmentInstance(position: Int): Fragment? {
if (containerId == 0) throw IllegalArgumentException("Container id is 0")
return fragmentManager.findFragmentByTag("android:switcher:$containerId:$position") return fragmentManager.findFragmentByTag("android:switcher:$containerId:$position")
} }
@ -23,11 +23,6 @@ class BaseFragmentPagerAdapter(private val fragmentManager: FragmentManager) : F
this.pages.putAll(pages) this.pages.putAll(pages)
} }
override fun instantiateItem(container: ViewGroup, position: Int): Any {
containerId = container.id
return super.instantiateItem(container, position)
}
override fun getItem(position: Int) = pages.keys.elementAt(position) override fun getItem(position: Int) = pages.keys.elementAt(position)
override fun getCount() = pages.size override fun getCount() = pages.size

View File

@ -59,10 +59,13 @@ class GradeFragment : BaseSessionFragment(), GradeView, MainView.MainChildView,
} }
override fun initView() { override fun initView() {
pagerAdapter.addFragmentsWithTitle(mapOf( pagerAdapter.apply {
containerId = gradeViewPager.id
addFragmentsWithTitle(mapOf(
GradeDetailsFragment.newInstance() to getString(R.string.all_details), GradeDetailsFragment.newInstance() to getString(R.string.all_details),
GradeSummaryFragment.newInstance() to getString(R.string.grade_menu_summary) GradeSummaryFragment.newInstance() to getString(R.string.grade_menu_summary)
)) ))
}
gradeViewPager.run { gradeViewPager.run {
adapter = pagerAdapter adapter = pagerAdapter

View File

@ -38,11 +38,14 @@ class LoginActivity : BaseActivity(), LoginView {
} }
override fun initAdapter() { override fun initAdapter() {
loginAdapter.addFragments(listOf( loginAdapter.apply {
containerId = loginViewpager.id
addFragments(listOf(
LoginFormFragment.newInstance(), LoginFormFragment.newInstance(),
LoginSymbolFragment.newInstance(), LoginSymbolFragment.newInstance(),
LoginStudentSelectFragment.newInstance() LoginStudentSelectFragment.newInstance()
)) ))
}
loginViewpager.run { loginViewpager.run {
offscreenPageLimit = 2 offscreenPageLimit = 2

View File

@ -46,11 +46,14 @@ class MessageFragment : BaseFragment(), MessageView, MainView.TitledView {
} }
override fun initView() { override fun initView() {
pagerAdapter.addFragmentsWithTitle(mapOf( pagerAdapter.apply {
containerId = messageViewPager.id
addFragmentsWithTitle(mapOf(
MessageTabFragment.newInstance(RECEIVED) to getString(R.string.message_inbox), MessageTabFragment.newInstance(RECEIVED) to getString(R.string.message_inbox),
MessageTabFragment.newInstance(SENT) to getString(R.string.message_sent), MessageTabFragment.newInstance(SENT) to getString(R.string.message_sent),
MessageTabFragment.newInstance(TRASHED) to getString(R.string.message_trash) MessageTabFragment.newInstance(TRASHED) to getString(R.string.message_trash)
)) ))
}
messageViewPager.run { messageViewPager.run {
adapter = pagerAdapter adapter = pagerAdapter