Search code examples
javaandroidandroid-intentonbackpressed

Have to press Back button two times


I am trying to implement simple back button on activity to go to previous activity, but some weird behavior is happening when I am calling method finish() - the problem is I have to press back button TWO times? Why is this happening? In the back method i have only finish(). What is the other way to go to previous saved activity without instantiate a completely new Intent?

  public void back1 (View view){
    this.finish();
    }

This is second try with the same result:

 ImageButton buttonback = (ImageButton) findViewById(R.id.imageButton6);

    buttonback.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onBackPressed();
        }
    });

Solution

  • You are most likely starting the activity twice.

    When pressing back you are finishing one, and the other one is coming forward. Check your onCreate and onResume for anything that may be using intents or starting any activity