开发者

Android Back Button functionality

I'm little bit fed up with the device back button functionality.

I have two activities. From 1st I go to 2nd and display one item through array in ListView and when I presses the back button i came back to 1st activity.

when I again go to 2nd activity to display the item once again it showing me the previous items at 1st row and then the same item in the second row as well.

Means it is not removing the previous item when i presses the back button.

I have used:

 public void onBackPressed ()
  开发者_开发百科{
      for(int i=0; i<sub_categories.length;i++)
      {
          sub_categories[i]="";
      }
      Log.d(this.getClass().getName(), "*****************back button pressed");

  }


I'm not entirely sure what you are trying to do, since you are not describing where you actually delete your item.

If you really always want to delete the item you have highlighted upon leaving activity no 2, you could delete it in onStop().

If you don't want to remember the state when leaving the activity you should set android:saveEnabled="false" in your layout manifest.


You need to notify the adapter that the data has changed using notifyDataSetChanged. And the adapter will tell the ListView to update it's view.


Here is how you can run code on key press:

@Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Log.d("BACK KEY PRESSED");
    }
    return super.onKeyDown(keyCode, event);
 }

After returning to an activity, use notifyDatasetChanged in the activity onResume() to update the list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜