0

In my custom view I use the below simple method to change child views dynamically:

    private void changeChildView(final View oldView, final View newView) {
    final int viewIndex = indexOfChild(oldView);
    removeViewAt(viewIndex);
    addView(newView, viewIndex);
}

In the beginning childView1 is placed on on the ViewGroup. As soon as the user triggers a gesture on childView1, I do following steps:

  1. I store childView1 in a global variable in my custom view: mTempView = childView1

  2. then I inflate childview2

  3. changeChildView(childView1, childView2)

It works like a charm. But if user hits back button I need to revert back to childView1 that is supposed to be stored in the global variable at this point. I do revert by:

changeChildView(childView2, mTempView)

But the revert doesn't work. I checked that the mTempView contains the view but it never shows up. Strangely it shows up as soon as I rotate which means that the the view was successfully added back in the revert process.

abedfar
  • 1,989
  • 24
  • 21
  • Are you getting the right value for `viewIndex`? When you change orientation the activity is recreated; depending on you code, that could also mean that things are going back to *default* (i.e., before you ran `changeChildView` for the first time). – natario Apr 26 '15 at 13:09
  • @mvai Thank you for your comment. Yes I get right index in both orientations. Answering your second question; as I have multiple child views I verified that the view that shows up after orientation-change is exactly the previous one, and not the default one (before running change for the first time) – abedfar Apr 26 '15 at 13:21
  • I can't help you - there might be some issue elsewhere in your code. Try posting some relevant code (custom ViewGroup, calls to `changeChildView`, activity onCreate/Start/Resume...) and see if someone helps. – natario Apr 26 '15 at 13:28

0 Answers0