Try opening two Activity in the stack

1

I have an Activity which when calling another Activity normally calls the other screen, the problem I have to exit the other screen twice to return to the first. I'm calling Activity in an event of a EditText , while enter on the keyboard:

campoBusca.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View arg0, int onKey, KeyEvent arg2) {
            // se pressionar enter
            if (onKey == KeyEvent.KEYCODE_ENTER) {
                //chama a tela passando por parametro a url
                Intent telaSegmento = new Intent(MainActivity.this, SegmentoView.class);
                Bundle bundleParametro = new Bundle();

                URL = "www.xxx";
                bundleParametro.putString("id", URL);

                telaSegmento.putExtras(bundleParametro);
                startActivity(telaSegmento);

                return false;
            }
        }
    }

Activity 2:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // carrega o layout onde contem o ListView
    setContentView(R.layout.empresa_lista);
    ActionBar bar = getActionBar();
    bar.hide();

    Intent dadosRecebidos = getIntent();
    if (dadosRecebidos != null) {
        Bundle parRecebidos = dadosRecebidos.getExtras();
        if (parRecebidos != null) {
            URL = parRecebidos.getString("id");
        }
    }
}

@Override
public void onBackPressed() {
    finish();
}

When invoking the onBackPressed() method, it closes the activity, but it is as if it were another, so I have to send it out again, then it goes back to the first Activity . Someone help me?

    
asked by anonymous 04.12.2014 / 20:37

1 answer

1

You are currently overwriting a method that in your case would not even need to overwrite, which would be onBackPressed() , you should remove this event from your code because it internally already finalizes the activity, in the correct way.

After removing this event, note that when you open your activity and then click back, your activity will close normally.

Additional:

To better understand your, depending on what you want, you can call an Activity in different ways, such as if you just start the Activity (which is what you are currently doing):

Intent i = new Intent(MainActivity.this, SegmentoView.class);            
startActivity(i);

Or call an activity waiting for a result (when you close the activity it will drop in the onActivityResult event of the source activity (the one you called))

Intent i = new Intent(MainActivity.this, SegmentoView.class);            
startActivityForResult(i, 1); //sendo 1 para request code
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK){
        //
    }
}

And in the activity you called:

setResult(RESULT_OK);

Or even call an activity waiting for result, but sending parameters:

Intent i = new Intent(MainActivity.this, SegmentoView.class);            
i.putExtra("PARAMETRO", 1 );
startActivityForResult(i, 1); //sendo 1 para result code esperado

Then in onCreate() of the other activity you have:

Bundle extras    = getIntent().getExtras();
SeuParametroInt  = extras.getInt("PARAMETRO");

These explanations are just for understanding how the iteration between android activities works, I hope I have helped.

    
05.12.2014 / 13:00