El gesto de deslizar inicia varias instancias de la misma actividad

I am trying to implement a tab based view wherein the user can switch between the tabs using swipe gesture. However, multiple instances of the same activity open on swipe. How can I prevent this? Thanks

public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction() & MotionEvent.ACTION_MASK){

    case MotionEvent.ACTION_DOWN:
        initialx=event.getX();
        initialy=event.getY();
        break;

    case MotionEvent.ACTION_UP:
        break;

    case MotionEvent.ACTION_MOVE:
        finalx=event.getX();
        finaly=event.getY();

        if (initialx>(finalx+150.0)){

            RadioGroup Options = (RadioGroup) findViewById(R.id.vOptions);
            int selectedId = Options.getCheckedRadioButtonId();
            final EditText QuestionNo = (EditText)findViewById(R.id.vnumberofquestions);
            final EditText Time = (EditText)findViewById(R.id.vtimeperquestion);

            if ((QuestionNo.getText().toString().equals(" ")) || (Time.getText().toString().equals("")) ||(selectedId==-1)) { 
                Intent intenta=new Intent(VerbalSelect.this, TabDisplay.class );
                intenta.putExtra("index", 0);
                startActivity(intenta);
            }
            else {   
                RadioButton selectedButton = (RadioButton) findViewById(selectedId);
                String testtype = (String) selectedButton.getText(); 
                int questionno = Integer.parseInt(QuestionNo.getText().toString());
                int timeperquestion = Integer.parseInt(Time.getText().toString());
                long timeroffset = questionno*timeperquestion;
                Intent intent=new Intent(VerbalSelect.this, Main.class);

                intent.putExtra("testtype", testtype);
                intent.putExtra("timeroffset",timeroffset);
                intent.putExtra("questionno","1");
                intent.putExtra("questionlimit", questionno);
                startActivity(intent);
            }
            break;
        }
    }
    return true;
}

preguntado el 26 de agosto de 12 a las 17:08

1 Respuestas

Echa un vistazo a ViewPager in the Android support library. Most UIs of this form that you see on a stock Android device use this component.

Each page is implemented either as a Fragment or layout of Views instead of a full Activity. This allows for clean swiping and animation between pages and avoids the issue of multiple activities that you're having.

To supply integration with tabs you can use PagerTabStrip as provided out of the box, or for more traditional style tabs see the code examples in the Support4Demos sample included with the SDK. It can be found at <sdk>/extras/android/compatibility/v4/samples/Support4Demos wherever you installed the SDK. The FragmentTabsPager demo is what you're looking for.

Respondido 26 ago 12, 19:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.