简体   繁体   中英

Android: parent activity data is lost after back from child activity when I press system back menu

I have following 3 activities:

  1. Groups
  2. Items
  3. Item Details

I am using SQLite to manage data. When I press a group, first 10 items of the specified groups will appear at Items activity. Pressing load more button, next 10 items will appear and so on. And when I press an Item on Item Activity, Item details will appear at Item Details Activity. I have set Items Activity as parent for Items Details Activity for Menu Action Bar.

The problem is when I press system back button at Item Details Activity, Items Activity screen is appearing with Empty data. The screen should be displayed with Items of the specified groups. I noticed that onCreate function is being called after pressing back button. But, the screen should display previous result set.

How to display previous result set at Items Activity?

In your ItemDetails activity put this code.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5
        && keyCode == KeyEvent.KEYCODE_BACK
        && event.getRepeatCount() == 0) {
    Log.d("CDA", "onKeyDown Called");
    onBackPressed();
    return true; 
}
return super.onKeyDown(keyCode, event);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM