teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを載せて欲しいとのことでしたので、載せました。よろしくお願いいたします。

2020/06/21 01:16

投稿

yukari00
yukari00

スコア5

title CHANGED
File without changes
body CHANGED
@@ -5,4 +5,157 @@
5
5
  このすぐ下にログを入れたところ
6
6
  Log -> D/listeningSelectedCards: com.google.firebase.firestore.core.ListenerRegistrationImpl@c3b3b7b
7
7
 
8
- このように出ました。nullではないので、remove出来てないという認識でいいのでしょうか?それともこれはデタッチはできているのでしょうか?
8
+ このように出ました。nullではないので、remove出来てないという認識でいいのでしょうか?それともこれはデタッチはできているのでしょうか?
9
+
10
+ <コード>
11
+ locateEachCardメソッドにListenerRegistrationを設置しました。
12
+ フラグメントを閉じた時に呼び出されるOnGoBackOnGameSettingFragmentメソッドでlistenerRegistrationをデタッチしました。(はずだが、されていなかった?)
13
+
14
+ Fragmentのコードもさらに下の方に載せました。
15
+
16
+ ```ここに言語を入力
17
+ private fun locateEachCard() {
18
+
19
+ listeningWords = database.collection(dbCollection).document(keyword).collection("words").document(keyword)
20
+ .addSnapshotListener { it, e ->
21
+
22
+ if (e != null) return@addSnapshotListener
23
+ if (it == null || !it.exists()) return@addSnapshotListener
24
+
25
+
26
+ val wordsDataList = mutableListOf<WordsData>()
27
+ val hashmap = it["words"] as MutableList<HashMap<String, String>>
28
+
29
+ for(i in 0 .. 24){
30
+ wordsDataList.add(WordsData(hashmap[i]["word"], hashmap[i]["color"]))
31
+ }
32
+
33
+ listeningSelectedCards = database.collection(dbCollection).document(keyword).collection("selectedCards").addSnapshotListener { query, e ->
34
+
35
+ if(e != null) return@addSnapshotListener
36
+ val selectedCardList = mutableListOf<WordsData>()
37
+
38
+ supportFragmentManager.beginTransaction().remove(ResultFragment()).commit();
39
+
40
+ if(query == null || query.isEmpty){
41
+
42
+ remaining_red.setText("赤カードの残り枚数:")
43
+ remaining_blue.setText("青カードの残り枚数:")
44
+ red_number_of_remaining.setText("8")
45
+ blue_number_of_remaining.setText("7")
46
+
47
+ turn = Turn.RED_TEAM_TURN
48
+ text_which_team_turn.setText("赤チームのターンです")
49
+
50
+ } else{
51
+
52
+ willUpdate(query, selectedCardList)
53
+ }
54
+
55
+ newTurn()
56
+
57
+ val listItem = mutableListOf<String>()
58
+
59
+ val adapter = CardAdapter(wordsDataList, selectedCardList, object : CardAdapter.OnCardAdapterListener {
60
+ override fun OnClickCard(word: String, wordsData: WordsData, holder: CardAdapter.ViewHolder) {
61
+
62
+ showWhatYouClicked(listItem, word, wordsDataList)
63
+ }
64
+ })
65
+ recycler_view.layoutManager = GridLayoutManager(this, 5)
66
+ recycler_view.adapter = adapter
67
+
68
+ if(ifGameIsOver) showResultFragment()
69
+ }
70
+ }
71
+ }
72
+ .
73
+ .
74
+ .
75
+ .
76
+ .
77
+ .
78
+ .
79
+ .
80
+ override fun OnGoBackOnGameSettingFragment() {
81
+
82
+ listeningWords?.remove()
83
+ listeningSelectedCards?.remove()
84
+ listeningMembers?.remove()
85
+ Log.d("listeningSelectedCards", "$listeningSelectedCards" //D/listeningSelectedCards: com.google.firebase.firestore.core.ListenerRegistrationImpl@c3b3b7bと出ました
86
+
87
+ teamToCollectAllCards = ""
88
+ teamGotGray = null
89
+ ifGameIsOver = false
90
+
91
+ //下ではfirebaseの”selectedCards"というコレクションにあるドキュメントを一つ一つ処理しています。(もっと効率な方法あったら教えてください)
92
+
93
+ database.collection(dbCollection).document(keyword).collection("selectedCards").get().addOnSuccessListener {
94
+ for( i in 0 until it.documents.size){
95
+ val documentId = it.documents[i].id
96
+ database.collection(dbCollection).document(keyword).collection("selectedCards").document(documentId).delete()
97
+ }
98
+
99
+ recycler_view.layoutManager = null
100
+ recycler_view.adapter = null
101
+ btn_explain.visibility = View.INVISIBLE
102
+ text_which_team_turn.setText("")
103
+ remaining_red.setText("")
104
+ remaining_blue.setText("")
105
+ blue_number_of_remaining.setText("")
106
+ red_number_of_remaining.setText("")
107
+
108
+ importWordsFromCSV()
109
+ supportFragmentManager.beginTransaction()
110
+ .replace(R.id.container_game, GameSettingFragment.newInstance(keyword, nickname))
111
+ .commit()
112
+ }
113
+ }
114
+ ```
115
+
116
+ ゲーム終了時のResultフラグメントです。(ここからOnGoBackOnGameSettingFragmentメソッドを呼び出すので、載せておきます。)
117
+ ```ここに言語を入力
118
+ class ResultFragment : Fragment() {
119
+ .
120
+ .
121
+ .
122
+ override fun onActivityCreated(savedInstanceState: Bundle?) {
123
+ .
124
+ .
125
+ .
126
+ btn_another_game_start.setOnClickListener {
127
+ database.collection(dbCollection).document(keyword).update("readyForAnotherGame", true)
128
+
129
+ }
130
+
131
+ btn_back_game_setting.setOnClickListener {
132
+ database.collection(dbCollection).document(keyword).update("readyToEndGame", true)
133
+
134
+ }
135
+
136
+ listening = database.collection(dbCollection).document(keyword).addSnapshotListener { it, e ->
137
+ if (e != null) return@addSnapshotListener
138
+ if (it == null ) return@addSnapshotListener
139
+
140
+ val endGame = it.getBoolean("readyToEndGame")?: false
141
+ Log.d("endGame", "$endGame")
142
+ if(endGame){
143
+ listener?.OnGoBackOnGameSettingFragment()
144
+ getFragmentManager()?.beginTransaction()?.remove(this)?.commit()
145
+ return@addSnapshotListener
146
+ }
147
+
148
+ val tryAnotherGame = it.getBoolean("readyForAnotherGame")?: false
149
+ Log.d("tryAnotherGame", "$tryAnotherGame")
150
+ if(tryAnotherGame){
151
+ listener?.OnStartAnotherGame(turnCount)
152
+ getFragmentManager()?.beginTransaction()?.remove(this)?.commit()
153
+ return@addSnapshotListener
154
+ }
155
+
156
+ }
157
+ }
158
+
159
+ .
160
+ .
161
+ ```