1 message in com.googlegroups.android-beginnerssetContentView on view1, then view2, ...
FromSent OnAttachments
ebs2...@gmail.com27 Feb 2008 15:02 
Subject:setContentView on view1, then view2, then view1 causes crash
From:ebs2...@gmail.com (ebs2@gmail.com)
Date:02/27/2008 03:02:14 PM
List:com.googlegroups.android-beginners

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.