Question

I've been searching this all over but no idea what happens...

when I come back to a previous activity my components are correctly initialized, but they have a new one on top of them...

I mean, on debug mode, I follow and I do the onResume code, the texts are written and the onCLicklisteners set, but an empty component is shown on top of the good one, and don't know why

of course when the app is first created it works fine!

any idea?

relevant code: the component

public class TopLayout extends RelativeLayout {

  public static final int BTN_LEFT = 0;
  public static final int BTN_RIGHT = 1;
  public static final int LBL_TITLE = 2;
  protected Button btnLeft;
  protected Button btnRight;
  protected TextView lblTitle;

  public TopLayout(Context _context, AttributeSet _attrs) {
    super(_context, _attrs);
  }

  public TopLayout(Context _context) {
    super(_context);
  }

  public TopLayout(Context _context, AttributeSet _attrs, int _defStyle) {
    super(_context, _attrs, _defStyle);
  }

  public void initialize(){
    String infService = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater li = (LayoutInflater) getContext().getSystemService(infService);
    li.inflate(R.layout.top_layout, this, true);

    btnLeft = (Button)findViewById(R.id.btnLeft);
    btnRight = (Button)findViewById(R.id.btnRight);
    lblTitle = (TextView)findViewById(R.id.lblTitle);
  }

  public void setText(int _element, String _text){
    switch (_element) {
    case BTN_LEFT:
        btnLeft.setText(_text);
        break;
    case BTN_RIGHT:
        btnRight.setText(_text);
        break;
    case LBL_TITLE:
        lblTitle.setText(_text);
        break;
    default:
        throw new RuntimeException("Unknown element to set Text: " + _text);
    }
  }

  public void hideButton(int _element){
    switch (_element) {
    case BTN_LEFT:
        btnLeft.setVisibility(Button.GONE);
        break;
    case BTN_RIGHT:
        btnRight.setVisibility(Button.GONE);
        break;
    default:
        throw new RuntimeException("Unknown button to hide: " + _element);
    }
  }

  public void setClickAction(int _element, OnClickListener _listener){
    switch (_element) {
    case BTN_LEFT:
        btnLeft.setOnClickListener(_listener);
        break;
    case BTN_RIGHT:
        btnRight.setOnClickListener(_listener);
        break;
    case LBL_TITLE:
        lblTitle.setOnClickListener(_listener);
        break;
    default:
        throw new RuntimeException("Unknown button to activate: " + _element);
    }
  }
}

the onResume of my activities:

topLyt = (TopLayout) findViewById(R.id.lytTop);
topLyt.initialize();
topLyt.setText(TopLayout.BTN_LEFT, res.getString(R.string.edit));
topLyt.setText(TopLayout.LBL_TITLE, res.getString(R.string.involved_people));
topLyt.setText(TopLayout.BTN_RIGHT, res.getString(R.string.add));

but the thing is, the first time it works, the second I get above this first one another uninitialized object (it got to be another one, because the object number is the same while debugging, and we can see the previous one if I put seethrough background to the buttons)

cannot post images because of reputation but please have a look here:

https://dl.dropbox.com/u/1668197/header.png

Was it helpful?

Solution

Call inititialize() method of your custom view in the view constructors only, no need to call it on onResume() method of the activity.

Here is a good read about custom views on Android platform.

OTHER TIPS

gpasci is right, you are inflating R.layout.top_layout in your initialize() method. So on any onResume() you'll add something to your TopLayout object. Call this code in onCreate or even betters as proposed by gpasci, call it in your constructors.

  public TopLayout(Context _context, AttributeSet _attrs) {
    this(_context, _attrs, null);

  }

  public TopLayout(Context _context) {
    this(_context, null);
  }

  public TopLayout(Context _context, AttributeSet _attrs, int _defStyle) {
    super(_context, _attrs, _defStyle);
    initialize()
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top