Please trust me, i really already tried so many things i found on the web, including this, this and the listview.onSaveInstanceState(), but unsuccessfully. I'll post some of my methods:
public class ChatFragment extends Fragment implements OnItemClickListener,
OnItemLongClickListener {
private String target;
private ListView listview;
private EditText edittext;
private Conversa conversa;
private MainActivity activity;
private int position_recover = -1;
public static final String LISTPOSITION = "listposition";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.chat_layout, container, false);
target = getArguments().getString(MyService.EXTRA_TARGET);
// esse target é passado com o mTag no contrutor do tablistener.
// no tablistener ele precisa ser igual definido na tab da activity, mas
// daqui pra baixo eh target
if (target.equalsIgnoreCase(MainActivity.TITLE_TAB_CHAT)) {
target = MyService.CANAL;
}
activity = (MainActivity) getActivity();
conversa = MyService.getConversa(target);
listview = (ListView) view.findViewById(R.id.chatlist);
edittext = (EditText) view.findViewById(R.id.chatinput);
listview.setAdapter(conversa);
listview.setOnItemClickListener(this);
listview.setOnItemLongClickListener(this);
// LISTENER QUE RECEBERA O "ENVIAR" DO TECLADO DO ANDROID
edittext.setOnEditorActionListener(...);
if (savedInstanceState != null) {
position_recover = savedInstanceState.getInt(LISTPOSITION);
}
return view;
}
@Override
public void onResume() {
super.onResume();
setHint();
if (position_recover != -1) {
listview.setSelection(position_recover);
position_recover = -1;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(LISTPOSITION, listview.getLastVisiblePosition());
super.onSaveInstanceState(outState);
}
After rotation, if listiview was on start position, it stills on start position, else it goes to the end.
Please help me cause i'm on it has been a while.