1
I have a Activity that when calling another Activity usually calls the other screen, the problem I have to leave the other screen twice to return to the first. I’m calling Activity in an event of a EditText
, to da 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 method onBackPressed()
, he closes the activity, but it is as if there is another, then I have to send out again, then he comes back to first Activity. Somebody help me?
Are you sure the onKey method isn’t starting two activities? Besides that I think it is not necessary to call the Finish method in onBackPressed, it already does it for you. It lacked to call the
super.onBackPressed
, that would take the place offinish
.– Wakim
The method is the one I showed you, yes, I have to leave twice, to go back to the previous screen, I already called this Action from another place, and I only need to press 1 time to leave, but the way I’m calling you there calling two.
– War Lock
No need to overwrite the method
onBackPressed()
in this case. You can remove this method from the code. The default implementation of this method already callsfinish()
.– Piovezan
@Wakim I think the problem is that same you spoke, the method
onKey()
should be called twice, one when the Enter key is pressed and the other when it is released. Why not complement your comment and convert it to answer? Just callif (onKey == KeyEvent.KEYCODE_ENTER && arg2.getAction() == KeyEvent.ACTION_DOWN) {
– Piovezan