I am working on an application that needs to pass along a number of
complex data objects across multiple views. My first approach was to
create a new activity from each view, and accessing a static version
of my custom object. This technique was bothering me because global
objects are typically bad, and it was becoming a hassle to deal with.
My next approach was to pass along all the data information through
the intent, and re-constructing the object each time. This caused a
noticeable delay, and was scrapped.
My next approach, which I think should be successful, was to just swap
between the various views as I needed them. I would call
setContentView(R.layout.view1), then setContentView(R.layout.view2),
etc, and everything was working fine.
However, the back button would exit the application when pressed,
rather than switching to the previous view, so I implemented a stack
and tried the following:
public void openView(int viewID) {
viewStack.push(new Integer(viewID));
setContentView(viewID);
}
public void closeView() {
viewStack.pop();
setContentView(viewStack.peek().intValue());
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
closeView();
return true;
}
return false;
}
Calling openView(R.layout.view1) works. Calling
openView(R.layout.view2) works. Calling closeView() afterwards pulls
the correct viewID from the stack, but causes a NullPointerException.