Domanda

I have a problem in a very simple application.

I have a main activity, and on button click I open a second activity:

newEntryButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            Intent intent = new Intent(MainActivity.this, NewSpendingActivity.class);
            MainActivity.this.startActivity(intent);
            return true;
        }
    });

When I close this activity (for example by tapping the back button) or by calling finish() the view loads once again. It only closes if I tap the back button again. What may be the cause of this?

È stato utile?

Soluzione

It's probably because you're using an OnTouchListener, which triggers on press, move and release. Try using an OnClickListener instead.

Altri suggerimenti

This should not give you problems:

newEntryButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent activityChangeIntent = new Intent(MainActivity.this,NewSpendingActivity.class);
            MainActivity.this.startActivity(activityChangeIntent);

        }
    });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top