0

I have 3 screens, Screen 1 , Screen 2, Screen 3.

This is the kind of back navigation that I am implementing,

Screen 3 -> Screen 2 -> Screen 1.

Here is my below code for Screen 1

public class Screen1 extends Activity implements OnClickListener{
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch(v.getId())
        {
            // This is where the intent starts to display the second Screen
            case R.id.button1:
                Intent started = new Intent(Screen1.this,Screen2.class);
                startActivity(started);
                break;
        }
    }
}

Screen 2

public class Screen2 extends Activity implements OnTouchListener{
    ListView displaylist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.screen2);

        // Please ignore this part of the code it just displays the list of items

        displaylist  = (ListView)findViewById(R.id.listView1);

        String[] listofitems = {"A","B","C","W"};

        ArrayAdapter<String> lists = new ArrayAdapter<String>(Screen2.this, android.R.layout.simple_list_item_1, listofitems);

        displaylist.setAdapter(lists);

        displaylist.setOnTouchListener(this);
    }

    // This is where the intent starts to display the Third Screen
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub

        Intent started2= new Intent(Screen2.this,Screen3.class);
        startActivity(started2);
        return true;
    }
}

Screen 3

public class Screen3 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Displays some layout
    }

}

My Current Problem

My current problem is that the back navigation(when I press back button) is in the following order

Screen3 -> Screen3 -> Screen2 -> Screen1

My Question

Why does the Screen3 display again when I press the back button ? Is this somekind of bug ?

From a similar question David Wasser comment says that

If your navigation is standard, then 1 starts 2 and the BACK button goes back to 1. 2 starts 3 and the BACK button goes back to 2. You don't need to do anything special to get this

Testing Device

Android Emulator

API Level:19

Note: I do not want to do this with Actionbar

gotwo
  • 663
  • 8
  • 16

1 Answers1

2

I think it's because your onTouch event is being fired twice, when pressing and releasing.

Try this, or use some other event:

public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
    Intent started2= new Intent(Screen2.this,Screen3.class);
    startActivity(started2);
    break;

case MotionEvent.ACTION_MOVE:
    // touch move code
    break;

case MotionEvent.ACTION_UP:
    // touch up code
    break;
}
return true;
}
Benten
  • 1,014
  • 12
  • 17