Fix empty chat list on rotation

This commit is contained in:
Sylvain Berfini 2016-05-13 12:14:09 +02:00
parent 924b67f01c
commit 08b763479a

View file

@ -145,6 +145,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
//Initialize UI //Initialize UI
contactName = (TextView) view.findViewById(R.id.contact_name); contactName = (TextView) view.findViewById(R.id.contact_name);
messagesList = (ListView) view.findViewById(R.id.chat_message_list); messagesList = (ListView) view.findViewById(R.id.chat_message_list);
messagesList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
searchContactField = (EditText) view.findViewById(R.id.search_contact_field); searchContactField = (EditText) view.findViewById(R.id.search_contact_field);
resultContactsSearch = (ListView) view.findViewById(R.id.result_contacts); resultContactsSearch = (ListView) view.findViewById(R.id.result_contacts);
@ -250,10 +251,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
return view; return view;
} }
public String getSipUri() {
return sipUri;
}
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
if (message != null) { if (message != null) {
@ -327,28 +324,28 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
ArrayList<LinphoneChatMessage> history; ArrayList<LinphoneChatMessage> history;
Context context; Context context;
public ChatMessageAdapter(Context context) { public ChatMessageAdapter(Context c) {
this.context = context; context = c;
history = new ArrayList<LinphoneChatMessage>();
refreshHistory(); refreshHistory();
} }
public void destroy() { public void destroy() {
history.clear();
history = null;
notifyDataSetInvalidated();
}
public void refreshHistory() {
if (history != null) { if (history != null) {
history.clear(); history.clear();
} }
}
public void refreshHistory() {
history.clear();
LinphoneChatMessage[] messages = chatRoom.getHistory(); LinphoneChatMessage[] messages = chatRoom.getHistory();
history = new ArrayList<LinphoneChatMessage>(Arrays.asList(messages)); history.addAll(Arrays.asList(messages));
messages = null; notifyDataSetChanged();
} }
public void addMessage(LinphoneChatMessage message) { public void addMessage(LinphoneChatMessage message) {
history.add(message); history.add(message);
notifyDataSetChanged();
} }
@Override @Override
@ -474,16 +471,14 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
public void dispayMessageList() { public void dispayMessageList() {
messagesList.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
if(chatRoom != null) { if(chatRoom != null) {
if (adapter != null) { if (adapter != null) {
adapter.refreshHistory(); adapter.refreshHistory();
adapter.notifyDataSetChanged();
} else { } else {
adapter = new ChatMessageAdapter(getActivity().getApplicationContext()); adapter = new ChatMessageAdapter(getActivity().getApplicationContext());
messagesList.setAdapter(adapter);
} }
} }
messagesList.setAdapter(adapter);
} }
private void displayChatHeader(LinphoneAddress address) { private void displayChatHeader(LinphoneAddress address) {
@ -551,13 +546,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
} }
return true; return true;
} }
@Override
public void onDestroy() {
if (adapter != null) adapter.destroy();
adapter = null;
super.onDestroy();
}
@Override @Override
public void onPause() { public void onPause() {
@ -577,12 +565,23 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
//Hide keybord //Hide keybord
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(message.getWindowToken(), 0); imm.hideSoftInputFromWindow(message.getWindowToken(), 0);
super.onPause(); super.onPause();
} }
@Override
public void onDestroy() {
if (adapter != null) {
adapter.destroy();
}
super.onDestroy();
}
@SuppressLint("UseSparseArrays") @SuppressLint("UseSparseArrays")
@Override @Override
public void onResume() { public void onResume() {
super.onResume();
message.addTextChangedListener(textWatcher); message.addTextChangedListener(textWatcher);
addVirtualKeyboardVisiblityListener(); addVirtualKeyboardVisiblityListener();
@ -619,7 +618,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
resultContactsSearch.setVisibility(View.GONE); resultContactsSearch.setVisibility(View.GONE);
remoteComposing.setVisibility(chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE); remoteComposing.setVisibility(chatRoom.isRemoteComposing() ? View.VISIBLE : View.GONE);
} }
super.onResume();
} }
private void selectAllList(boolean isSelectAll){ private void selectAllList(boolean isSelectAll){
@ -801,7 +799,6 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneC
private void invalidate() { private void invalidate() {
adapter.refreshHistory(); adapter.refreshHistory();
adapter.notifyDataSetChanged();
chatRoom.markAsRead(); chatRoom.markAsRead();
} }