I use fragments (ListFragment) in an activity and i save the selected item to restore it when the screen rotates.
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
}
When the user clicks on "back" I don't want to save the item selection. I need to override the back button and clear the bundle instance passed to my activity.
I don't really know how to get the bundle instance, I've tried to use a global var but when I use it i have a NullPointerException.
Here is how i do :
private Bundle bundle;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
bundle.clear();
}
return super.onKeyDown(keyCode, event);
}
Where can i retrieve the information I want (currentListIndex): in the onCreate() of my activity / onCreateView() of my detail fragment ?
ok I have found the solution.
In my class that extends ListFragment :
// Restore last state for checked position (when screen orientation
// change or activity resumed)
if (savedInstanceState != null) {
mCurrentSelectedItemIndex = savedInstanceState.getInt(
"currentListIndex", -1);
}
In my activity :
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AccountBDD accountBdd = application.getAccountBdd();
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
mCurrentSelectedItemIndex = -1;
}
return super.onKeyDown(keyCode, event);
}
and it works
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.