In my app I have activity which contains two fragments.
The first fragment contains ViewPager which contains a few fragments(the first fragment of viewpager fragments contains ListView). The second fragment is simple settings fragment. For example, I have checked the second element on the ListView, I click settings and my second fragment from activity opens. I click back button and I go back to my ListView, then I call :
listView.setItemChecked(0, true);
listView.setSelection(0);
But this does not work, the second element is still checked... I go from ViewPager to the second fragment in below way:
getSupportFragmentManager().beginTransaction()
.replace(R.id.slpdActivityLayout, fragment).addToBackStack(null).commit();
Why my selection does not work only when I press back button?
It's about the lifecycle of your fragment like seen -> here <- you cant do UI updates in onCreateView
. so place your code to onStart
or better onResume
.
remove:
public void onCreateView() {
listView.setItemChecked(0, true);
listView.setSelection(0);
}
add:
public void onResume() {
listView.setItemChecked(0, true);
listView.setSelection(0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.