Intent opening two Activity in stack

Asked

Viewed 613 times

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 of finish.

  • 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.

  • 1

    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 calls finish().

  • 1

    @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 call if (onKey == KeyEvent.KEYCODE_ENTER && arg2.getAction() == KeyEvent.ACTION_DOWN) {

1 answer

1


You are currently overwriting a method that in your case would not be necessary at all to overwrite, which would be the onBackPressed(), you should remove this event from your code because it in your internal already completes the activity in the correct way.

After removing this event note that when opening your activity and then clicking back your activity will close normally.

Additional:

For your better understanding, depending on what you want, you can call an Activity in different ways, such as if you just start 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 will fall into the event onActivityResult of the origin activity(which 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 the onCreate() of the other activity you have:

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

These explanations are only for the purpose of understanding how the iteration works between android activities, I hope to have helped.

  • The problem is not being caused by the superscript of onBackPressed(); the overwritten version is unnecessary but works as expected. See the comments of the question for the probable cause of the problem.

  • Well I answered this way because I had a similar problem a while ago, I’m waiting for the manifestation of the OP on

  • worked the way it was told up there, was the event Onkey q was wrong, about writing the method, thanks for the tip. thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.