Use the return hardware button to another function

Asked

Viewed 297 times

1

I have an app with several Activitys, whenever I change Activity I run the following commands:

Intent intent = new Intent(gerenciar2.this, excluir.class);
                intent.putExtra("tabbanco", tabbanco);
                intent.putExtra("pagina", page2);
                startActivity(intent);
                gerenciar2.this.finish();

Problem is that when I run the undo of the android hardware it closes the last open Activity thus closing the application.

So I stopped executing the command MainActivity.this.finish(); only in my main activity, so when the user uses undo it returns to the main activity.

Blz, so far so good.

Problem is that I step several times to my main activity and opens one on top of the other every time I change Activity.

I need to execute the startActivity(intent); to pass the updated parameters, so wanted before I do the startActivity(intent); I run a method that closes the main activity before I create it again. I did some research and I couldn’t find anything about it, so I’d like someone to give me an alternative to that. thanks!

What I wish I could do was press the button and come back from hers and ask if I want to leave!

2 answers

2


If I understand correctly, you don’t have to raise Activity main whenever you return there, just use the methods onRestoreInstanceState and onSaveInstanceState to pass the data from one side to the other without doing start and Finish of Activity.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    conf = savedInstanceState.getParcelable("conf");
    getBaseContext().getResources().updateConfiguration(conf,
            getBaseContext().getResources().getDisplayMetrics());


    tabbanco = savedInstanceState.getExtra("tabbanco");

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    Configuration conf = getResources().getConfiguration();

    outState.putExtra("tabbanco", tabbanco);

}

2

With great help from Orge, I arrived at the following I rewrote the function public void onBackPressed() to get back screen.

thus:

public void onBackPressed()  {
    Intent iplay = new Intent(paginadeedicao.this, MainActivity.class);
    iplay.putExtra("pagina2", page2);
    iplay.putExtra("tabbanco", tabbanco);
    startActivity(iplay);
    paginadeedicao.this.finish();
}

and upon arriving at the main I executed the following:

public void onBackPressed()  {
    AlertDialog.Builder mensagem = 
            new AlertDialog.Builder(MainActivity.this);

    mensagem.setTitle("Atenção!");
    mensagem.setMessage("Deseja realmente sair?");
    mensagem.setIcon(R.drawable.sair);

    mensagem.setPositiveButton("Sim",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            fechabanco();
            MainActivity.this.finish();

        }});
    mensagem.setNegativeButton("Não",null);
    mensagem.show();

}

Thank you Jorge!

Browser other questions tagged

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