fix InstantiationException for fragments

fragments must have a public, no-arg constructor.
extra information must be passed as arguments.

With a constructor that receives the data, the following error occurs:

Fragment$InstantiationException: Unable to instantiate fragment ..... could not find Fragment constructor

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-06-30 16:07:20 +02:00 committed by Andy Scherzinger (Rebase PR Action)
parent 3224ae1e1f
commit 1a000859bf
2 changed files with 42 additions and 14 deletions

View file

@ -25,9 +25,9 @@ import com.nextcloud.talk.polls.viewmodels.PollCreateViewModel
import javax.inject.Inject import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class) @AutoInjector(NextcloudTalkApplication::class)
class PollCreateDialogFragment( class PollCreateDialogFragment() : DialogFragment(), PollCreateOptionsItemListener {
private val roomToken: String
) : DialogFragment(), PollCreateOptionsItemListener { lateinit var roomToken: String
@Inject @Inject
lateinit var viewModelFactory: ViewModelProvider.Factory lateinit var viewModelFactory: ViewModelProvider.Factory
@ -42,6 +42,8 @@ class PollCreateDialogFragment(
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
viewModel = ViewModelProvider(this, viewModelFactory)[PollCreateViewModel::class.java] viewModel = ViewModelProvider(this, viewModelFactory)[PollCreateViewModel::class.java]
roomToken = arguments?.getString(KEY_ROOM_TOKEN)!!
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@ -150,10 +152,15 @@ class PollCreateDialogFragment(
*/ */
companion object { companion object {
private val TAG = PollCreateDialogFragment::class.java.simpleName private val TAG = PollCreateDialogFragment::class.java.simpleName
private const val KEY_ROOM_TOKEN = "keyRoomToken"
@JvmStatic @JvmStatic
fun newInstance( fun newInstance(roomTokenParam: String): PollCreateDialogFragment {
roomTokenParam: String val args = Bundle()
): PollCreateDialogFragment = PollCreateDialogFragment(roomTokenParam) args.putString(KEY_ROOM_TOKEN, roomTokenParam)
val fragment = PollCreateDialogFragment()
fragment.arguments = args
return fragment
}
} }
} }

View file

@ -19,12 +19,12 @@ import com.nextcloud.talk.polls.viewmodels.PollMainViewModel
import javax.inject.Inject import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class) @AutoInjector(NextcloudTalkApplication::class)
class PollMainDialogFragment( class PollMainDialogFragment() : DialogFragment() {
private val user: UserEntity,
private val pollId: String, lateinit var user: UserEntity
private val roomToken: String, lateinit var roomToken: String
private val pollTitle: String lateinit var pollId: String
) : DialogFragment() { lateinit var pollTitle: String
@Inject @Inject
lateinit var viewModelFactory: ViewModelProvider.Factory lateinit var viewModelFactory: ViewModelProvider.Factory
@ -37,6 +37,11 @@ class PollMainDialogFragment(
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
viewModel = ViewModelProvider(this, viewModelFactory)[PollMainViewModel::class.java] viewModel = ViewModelProvider(this, viewModelFactory)[PollMainViewModel::class.java]
user = arguments?.getParcelable(KEY_USER_ENTITY)!!
roomToken = arguments?.getString(KEY_ROOM_TOKEN)!!
pollId = arguments?.getString(KEY_POLL_ID)!!
pollTitle = arguments?.getString(KEY_POLL_TITLE)!!
} }
@SuppressLint("InflateParams") @SuppressLint("InflateParams")
@ -79,11 +84,13 @@ class PollMainDialogFragment(
} }
private fun showVoteScreen() { private fun showVoteScreen() {
val contentFragment = PollVoteFragment(
val contentFragment = PollVoteFragment.newInstance(
viewModel, viewModel,
roomToken, roomToken,
pollId pollId
) )
val transaction = childFragmentManager.beginTransaction() val transaction = childFragmentManager.beginTransaction()
transaction.replace(binding.messagePollContentFragment.id, contentFragment) transaction.replace(binding.messagePollContentFragment.id, contentFragment)
transaction.commit() transaction.commit()
@ -113,12 +120,26 @@ class PollMainDialogFragment(
* Fragment creator * Fragment creator
*/ */
companion object { companion object {
private const val KEY_USER_ENTITY = "keyUserEntity"
private const val KEY_ROOM_TOKEN = "keyRoomToken"
private const val KEY_POLL_ID = "keyPollId"
private const val KEY_POLL_TITLE = "keyPollTitle"
@JvmStatic @JvmStatic
fun newInstance( fun newInstance(
user: UserEntity, user: UserEntity,
roomTokenParam: String, roomTokenParam: String,
pollId: String, pollId: String,
name: String name: String
): PollMainDialogFragment = PollMainDialogFragment(user, pollId, roomTokenParam, name) ): PollMainDialogFragment {
val args = Bundle()
args.putParcelable(KEY_USER_ENTITY, user)
args.putString(KEY_ROOM_TOKEN, roomTokenParam)
args.putString(KEY_POLL_ID, pollId)
args.putString(KEY_POLL_TITLE, name)
val fragment = PollMainDialogFragment()
fragment.arguments = args
return fragment
}
} }
} }