Take the 2-minute tour ×

On my class, I am using the setcontentview(R.layout.main2) in which after a point on the class when a button is pressed am calling a second layout main3. I don't know, how to use a back button on the second layout in order to move back on the main2. I try to use the finish();, but it take me more back on the layout main1. Also, I try to call again the layout main2, but then my buttons are not working. How this can be done?


public class Conn extends Activity 

 public void onCreate(Bundle mSavedInstanceState) 

findViewById(R.id.Devices).setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {


        if (DeviceName==(null))
            DeviceName="Not Connected";
        final TextView BDConnected = (TextView) findViewById(R.id.BDConnected);
          BDConnected.setText("BD Device Connected :"+DeviceName);

          WifiManager wifiManager=(WifiManager) getSystemService(Context.WIFI_SERVICE);
        String net=wifiManager.getConnectionInfo().getSSID();
          final TextView WiFiConnected = (TextView) findViewById(R.id.WiFiConnected);
          WiFiConnected.setText("WiFi NetWork Connected :"+net);

on the oncreate am calling my main layout(main2). when the button devices is pressed from the main layout it forward me on the second layout (devices). from that point how i can go back on the layout main2?


i use viewswitches and merger the two layouts into one, and use switch.previous and next

share|improve this question
Some code samples would be nice, otherwise it's a bit hard to understand where you're going wrong. –  Egor Feb 13 '12 at 20:48
How many Activities are in your project? 3? "How to use a back button on the second layout in order to move back on the main2" The second layout isnt main2? Please give some code. –  Robert Hahn Feb 13 '12 at 20:48
Answer i use viewswitches and merger the two layouts into one, and use switch.previous and next –  prokopis Feb 14 '12 at 7:46

2 Answers 2

up vote 1 down vote accepted

First, you should be aware that in Android, the idea isn't really to keep one Activity and bounce between separate views using setContentView on keypresses. Generally (and yes, there are exceptions to this, but generally), you should have a different Activity for each screen you want to show the user- list screen, detail screen, etc. Then whenever the user wants to go back to a previous screen, they hit the back button, the current Activity is popped off the stack, and the previous one appears.

A really good resource to read on all this would be the Activity page of the Android Developer Reference, located here. Particularly relevant to you would be the "Activity Lifecycle" and "Starting Activities and Getting Results" sections.

share|improve this answer

you can writ like this for handling the back button

public void onBackPressed() {
    Intent backPressedIntent = new Intent();
    backPressedIntent .setClass(getApplicationContext(), main2.class);
    startActivity(backPressedIntent );
share|improve this answer

Your Answer


