开发者

Finishing activity first clears EditTexts and only then does what it should do

开发者 https://www.devze.com 2023-02-28 22:43 出处:网络
that\'s my first post here. I have problem with managing activities. I have my main activity, which launches other activities containing some EditTexts. Here\'s the code for launching:

that's my first post here. I have problem with managing activities. I have my main activity, which launches other activities containing some EditTexts. Here's the code for launching:

        Intent myIntent = new Intent(ActualClass.this, Next开发者_运维问答Class.class);
        startActivity(myIntent);

I have few EditTexts on the freshly launched activity and some buttons. One of the buttons gets me back to the main activity. On its OnTouchEvent I run simple finish(); It all seems simple and should work just fine, but when I click the button which should get me back to my main activity, it firstly clears my EditTexts. When I click it for the second time, It finally gets me back to my main activity. What should I do to prevent clearing the EditTexts and get back to my main activity after the first click? I'd really appreciate your help.

Sorry guys, I'm adding more code. So here's the listener which launches the second activity, the one with EditTexts:

public boolean onTouch(View v, MotionEvent me) {
        switch(v.getId()){

        case R.id.buttonForward:
            Intent myIntent = new Intent(ActualClass.this, NextClass.class);
            startActivity(myIntent);
            break;

        default:
            break;

        }
        return false;
    }

And on the second activity, where my problem seems to occur, the listener on button looks like this:

   buttonBack.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            finish();
            return false;
        }
    });

And the problem is: when I first click the back button on the second activity, it doesn't get me back to my main activity - it clears all the EditTexts on activity and sets the focus on first element. When I click the button for the second time, it finally gets me back to main activity.

I'm providing full code of the NextClass class, the one with EditTexts.

public class NextClass extends Activity implements OnTouchListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.nextclasslayout);

        final EditText firstEditText = (EditText) findViewById(R.id.firstEditTextName);
        final EditText secondEditText = (EditText) findViewById(R.id.secondEditTextName);

        ImageButton buttonBack = (ImageButton) findViewById(R.id.imageButtonBack);


        buttonBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                finish();
                return true;
            }
        });

    }    

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {

        return false;
    }

}


I think your EditTexts aren't getting cleared. You just launch two NextClass activities because onTouch() method is called twice: once for ACTION_DOWN and once for Action_UP. And when you finish the top-most one, the next one appears. If you use OnClickListener in ActualClass activity for handling click events from the button, everything should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消