Switch from main.xml layout to another layout

I have a basic Android question here:

I have a main.xml layout that loads when the application starts. There is a menu button on this page that I would like (when pressed) to send the user to another layout (about.xml).

I doubt it is correct. When you click this command, it is called:

setContentView(R.layout.about); 

And it seems to work, I see the page about.xml, but I can’t go back to the main.xml layout, when I press the BACK button on my Android device, the application just closes.

I doubt this is the correct way to navigate between xml layout files. Could you help or point me to a page that talks about this for a novice programmer, like me?

Many thanks,

Pat

EDIT: Thanks for all the answers you helped me point in the right direction. To help future noob programmers like me understand actions, here is a great simple tutorial that I found on the Internet that displayed it for us newbies!

http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/

+8
android xml switch-statement layout
source share
4 answers

Do you have a separate activity for your page? Typically, a new action is created for each screen. When you switch to a new screen, the new activity will be stacked on top of the first screen activity. When you click "Back" on your Android device, the previous action will be displayed.

+7
source share

What you want to do is create a new Activity for every other screen. You create each new action as a new class and use Intents to move between them. This way you will use setContentView(); only once for each action. Android website contains great resources.

+1
source share

Your main action has a contentview set to main.xml , and you have another about.xml that needs to be configured for another action so that you can move from one action to another using Intents. I suggest you kindly go through the developer's site, where you can find the use of activity and intentions.

+1
source share

When switching between a layout like this, you need to control the back buttons

Here is an example of pseudo code to use in your core activities

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if((keyCode == KeyEvent.KEYCODE_BACK) && (secondary_layout_is_displayed)) { displayPrimaryLayout(); // return true to let the system know we consumed the back button press return true; } // return the default value return super.onKeyDown(keyCode, event); } 
-2
source share

All Articles