Removed chat fragment caching.
This commit is contained in:
parent
cbcc0802dd
commit
137f337cfe
2 changed files with 14 additions and 33 deletions
|
@ -1,10 +1,6 @@
|
|||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@android:id/primary"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent">
|
||||
|
||||
<fragment class="com.nutomic.ensichat.fragments.ContactsFragment"
|
||||
android:id="@android:id/primary"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</FrameLayout>
|
||||
|
|
|
@ -10,8 +10,6 @@ import com.nutomic.ensichat.R
|
|||
import com.nutomic.ensichat.bluetooth.{ChatService, Device}
|
||||
import com.nutomic.ensichat.fragments.{ChatFragment, ContactsFragment}
|
||||
|
||||
import scala.collection.mutable.HashMap
|
||||
|
||||
/**
|
||||
* Main activity, entry point for app start.
|
||||
*/
|
||||
|
@ -21,8 +19,6 @@ class MainActivity extends Activity {
|
|||
|
||||
private var ContactsFragment: ContactsFragment = _
|
||||
|
||||
private val ChatFragments = new HashMap[Device.ID, ChatFragment]()
|
||||
|
||||
private var currentChat: Option[Device.ID] = None
|
||||
|
||||
/**
|
||||
|
@ -37,24 +33,22 @@ class MainActivity extends Activity {
|
|||
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE)
|
||||
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0)
|
||||
startActivityForResult(intent, RequestSetDiscoverable)
|
||||
|
||||
val fm = getFragmentManager
|
||||
if (savedInstanceState != null) {
|
||||
ContactsFragment = fm.getFragment(savedInstanceState, classOf[ContactsFragment].getName)
|
||||
.asInstanceOf[ContactsFragment]
|
||||
for (i <- 0 until savedInstanceState.getInt("chat_fragments_count", 0)) {
|
||||
val key = classOf[ChatFragment].getName + i
|
||||
val cf = fm.getFragment(savedInstanceState, key).asInstanceOf[ChatFragment]
|
||||
ChatFragments += (cf.getDevice -> cf)
|
||||
if (savedInstanceState.containsKey("current_chat")) {
|
||||
currentChat = Option(new Device.ID(savedInstanceState.getString("current_chat")))
|
||||
openChat(currentChat.get)
|
||||
}
|
||||
currentChat = Some(new Device.ID(savedInstanceState.getString("current_chat")))
|
||||
currentChat.collect{case c => openChat(c) }
|
||||
} else {
|
||||
ContactsFragment = new ContactsFragment()
|
||||
}
|
||||
fm.beginTransaction()
|
||||
.add(android.R.id.content, ContactsFragment)
|
||||
.commit()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves all fragment state.
|
||||
|
@ -62,13 +56,7 @@ class MainActivity extends Activity {
|
|||
override def onSaveInstanceState(outState: Bundle): Unit = {
|
||||
super.onSaveInstanceState(outState)
|
||||
getFragmentManager.putFragment(outState, classOf[ContactsFragment].getName, ContactsFragment)
|
||||
outState.putInt("chat_fragments_count", ChatFragments.size)
|
||||
var i: Int = 0
|
||||
ChatFragments.foreach(cf => {
|
||||
getFragmentManager.putFragment(outState, classOf[ChatFragment].getName + i, cf._2)
|
||||
i += 1
|
||||
})
|
||||
outState.putString("current_chat", currentChat.toString)
|
||||
currentChat.collect{case c => outState.putString("current_chat", c.toString)}
|
||||
}
|
||||
|
||||
override def onCreateOptionsMenu(menu: Menu): Boolean = {
|
||||
|
@ -105,13 +93,10 @@ class MainActivity extends Activity {
|
|||
*/
|
||||
def openChat(device: Device.ID): Unit = {
|
||||
currentChat = Some(device)
|
||||
val ft = getFragmentManager.beginTransaction()
|
||||
if (!ChatFragments.keySet.contains(device)) {
|
||||
ChatFragments += (device -> new ChatFragment(device))
|
||||
ft.add(android.R.id.content, ChatFragments.apply(device))
|
||||
}
|
||||
ft.detach(ContactsFragment)
|
||||
.attach(ChatFragments.apply(device))
|
||||
getFragmentManager
|
||||
.beginTransaction()
|
||||
.detach(ContactsFragment)
|
||||
.add(android.R.id.content, new ChatFragment(device))
|
||||
.commit()
|
||||
}
|
||||
|
||||
|
@ -122,7 +107,7 @@ class MainActivity extends Activity {
|
|||
if (currentChat != None) {
|
||||
getFragmentManager
|
||||
.beginTransaction()
|
||||
.detach(ChatFragments.apply(currentChat.get))
|
||||
.remove(getFragmentManager().findFragmentById(android.R.id.content))
|
||||
.attach(ContactsFragment)
|
||||
.commit()
|
||||
currentChat = None
|
||||
|
|
Reference in a new issue