From 9fcf245ecd31cd65842b204a4da806fefa8b4132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Tue, 12 Feb 2019 00:41:02 +0100 Subject: [PATCH] Fix uninitialized fragment after restoring the activity (#237) --- .../java/io/github/wulkanowy/data/db/AppDatabase.kt | 6 ++---- .../wulkanowy/ui/base/BaseFragmentPagerAdapter.kt | 9 ++------- .../wulkanowy/ui/modules/grade/GradeFragment.kt | 11 +++++++---- .../wulkanowy/ui/modules/login/LoginActivity.kt | 13 ++++++++----- .../wulkanowy/ui/modules/message/MessageFragment.kt | 13 ++++++++----- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt b/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt index 974d5dab..7dfd45fc 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/AppDatabase.kt @@ -6,16 +6,14 @@ import androidx.room.Room import androidx.room.RoomDatabase import androidx.room.RoomDatabase.JournalMode.TRUNCATE 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.AttendanceSummaryDao import io.github.wulkanowy.data.db.dao.ExamDao import io.github.wulkanowy.data.db.dao.GradeDao 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.LuckyNumberDao +import io.github.wulkanowy.data.db.dao.MessagesDao import io.github.wulkanowy.data.db.dao.NoteDao import io.github.wulkanowy.data.db.dao.SemesterDao 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.Grade 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.LuckyNumber +import io.github.wulkanowy.data.db.entities.Message import io.github.wulkanowy.data.db.entities.Note import io.github.wulkanowy.data.db.entities.Semester import io.github.wulkanowy.data.db.entities.Student diff --git a/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragmentPagerAdapter.kt b/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragmentPagerAdapter.kt index bccb8f78..fcf7e980 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragmentPagerAdapter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/base/BaseFragmentPagerAdapter.kt @@ -1,6 +1,5 @@ package io.github.wulkanowy.ui.base -import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentPagerAdapter @@ -9,9 +8,10 @@ class BaseFragmentPagerAdapter(private val fragmentManager: FragmentManager) : F private val pages = mutableMapOf() - private var containerId = 0 + var containerId = 0 fun getFragmentInstance(position: Int): Fragment? { + if (containerId == 0) throw IllegalArgumentException("Container id is 0") return fragmentManager.findFragmentByTag("android:switcher:$containerId:$position") } @@ -23,11 +23,6 @@ class BaseFragmentPagerAdapter(private val fragmentManager: FragmentManager) : F 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 getCount() = pages.size diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt index d56eca9a..7a1c67ee 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/grade/GradeFragment.kt @@ -59,10 +59,13 @@ class GradeFragment : BaseSessionFragment(), GradeView, MainView.MainChildView, } override fun initView() { - pagerAdapter.addFragmentsWithTitle(mapOf( - GradeDetailsFragment.newInstance() to getString(R.string.all_details), - GradeSummaryFragment.newInstance() to getString(R.string.grade_menu_summary) - )) + pagerAdapter.apply { + containerId = gradeViewPager.id + addFragmentsWithTitle(mapOf( + GradeDetailsFragment.newInstance() to getString(R.string.all_details), + GradeSummaryFragment.newInstance() to getString(R.string.grade_menu_summary) + )) + } gradeViewPager.run { adapter = pagerAdapter diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt index 6535f777..da054bb5 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/login/LoginActivity.kt @@ -38,11 +38,14 @@ class LoginActivity : BaseActivity(), LoginView { } override fun initAdapter() { - loginAdapter.addFragments(listOf( - LoginFormFragment.newInstance(), - LoginSymbolFragment.newInstance(), - LoginStudentSelectFragment.newInstance() - )) + loginAdapter.apply { + containerId = loginViewpager.id + addFragments(listOf( + LoginFormFragment.newInstance(), + LoginSymbolFragment.newInstance(), + LoginStudentSelectFragment.newInstance() + )) + } loginViewpager.run { offscreenPageLimit = 2 diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt index df6ceca7..7a890339 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/message/MessageFragment.kt @@ -46,11 +46,14 @@ class MessageFragment : BaseFragment(), MessageView, MainView.TitledView { } override fun initView() { - pagerAdapter.addFragmentsWithTitle(mapOf( - MessageTabFragment.newInstance(RECEIVED) to getString(R.string.message_inbox), - MessageTabFragment.newInstance(SENT) to getString(R.string.message_sent), - MessageTabFragment.newInstance(TRASHED) to getString(R.string.message_trash) - )) + pagerAdapter.apply { + containerId = messageViewPager.id + addFragmentsWithTitle(mapOf( + MessageTabFragment.newInstance(RECEIVED) to getString(R.string.message_inbox), + MessageTabFragment.newInstance(SENT) to getString(R.string.message_sent), + MessageTabFragment.newInstance(TRASHED) to getString(R.string.message_trash) + )) + } messageViewPager.run { adapter = pagerAdapter