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