回到以前的屏幕

时间:2010-09-05 20:02:41

标签: java android

我是Android编程的新手,我正在尝试使用startActivity打开另一个屏幕后,如何回到之前的屏幕/活动。从我所做的所有研究中,使用finish()方法应该在当前屏幕之前调出前一个屏幕;但是,在我的测试程序中(有四个屏幕带有“下一个”和“后退”按钮),当按下后退按钮时,它会一直跳回到第一个屏幕而不是前一个屏幕。

对此的任何帮助将不胜感激。我的代码如下:

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();
        }


     }

 }

1 个答案:

答案 0 :(得分:0)

官方API演示中的Recorder Activity演示可能会有所帮助。

以下是其源代码的链接。

Recorder Activity One (Launcher)

Recorder Activity Two

Recorder Activity Three

Recorder Activity Four (This is where it gets interesting)