简体   繁体   English

返回上一个屏幕

[英]Going Back to Previous Screens

I am new to Android programming and I'm trying to figure out how to go back to a previous screen/activity after opening another screen using startActivity. 我是Android编程的新手,我想弄清楚在使用startActivity打开另一个屏幕后如何返回上一个屏幕/活动。 From all the research I've done, using the finish() method should bring up the previous screen just before the current one; 从我所做的所有研究中,使用finish()方法应该在前一个屏幕之前显示当前屏幕。 however, in my test program (has four screens with a "next" & "back"button), when pressing the back button, it jumps all the way back to the first screen instead of going to the previous screen. 但是,在我的测试程序中(具有四个带有“下一个”和“后退”按钮的屏幕),当按下“后退”按钮时,它将一直跳回到第一个屏幕,而不是前一个屏幕。

Any help with this would be greatly appreciated. 任何帮助,将不胜感激。 My code is below: 我的代码如下:

public class Screen1 extends Activity implements OnClickListener {


    private Button next;
    private Button quit;



    public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.screen1);

     next = (Button) findViewById(R.id.next);
        quit = (Button) findViewById(R.id.quit);

        next.setOnClickListener(this);
        quit.setOnClickListener(this);
    }


    public void onClick(View view) {

      switch(view.getId()) {  

        case R.id.next: {
             startActivity(new Intent(this, Screen2.class));
        }
        case R.id.quit: {

             finish();
        }

      }

     }

 }




public class Screen2 extends Activity implements OnClickListener {


    private Button next;
    private Button back;



    public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.screen2);

     next = (Button) findViewById(R.id.next);
        back = (Button) findViewById(R.id.back);

        next.setOnClickListener(this);
        back.setOnClickListener(this);
    }


    public void onClick(View view) {

      switch(view.getId()) {  

        case R.id.next: {
             startActivity(new Intent(this, Screen3.class));
        }
        case R.id.back: {

             finish();
        }

      }

     }

 }





public class Screen3 extends Activity implements OnClickListener {


    private Button next;
    private Button back;



    public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.screen3);

     next = (Button) findViewById(R.id.next);
        back = (Button) findViewById(R.id.back);

        next.setOnClickListener(this);
        back.setOnClickListener(this);
    }


    public void onClick(View view) {

      switch(view.getId()) {  

        case R.id.next: {
             startActivity(new Intent(this, Screen4.class));
        }
        case R.id.back: {

             finish();
        }

      }

     }

 }




public class Screen4 extends Activity implements OnClickListener {



    private Button back;



    public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.screen4);

        back = (Button) findViewById(R.id.back);

        back.setOnClickListener(this);
    }


    public void onClick(View view) {

             finish();
        }


     }

 }

The Recorder Activity demo in the official API Demo might be of help. 官方API演示中的Recorder Activity演示可能会有所帮助。

Here are the links to their source code. 以下是其源代码的链接。

Recorder Activity One (Launcher) 记录器活动一(启动器)

Recorder Activity Two 记录器活动二

Recorder Activity Three 记录器活动三

Recorder Activity Four (This is where it gets interesting) 记录器活动四(这很有趣)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM