Use the hardware back button for another function

1

I have an app with several Activitys, whenever I change from Activity I execute 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 android hardware undo it closes the last opened activity thus closing the application.

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

Blz, so far so good.

Problem is that I go several times to my main activity and open one over the other every time I change from Activity.

I need to run startActivity(intent); to pass the updated parameters, so I wanted it before I did startActivity(intent); I would execute a method that closes the activity main before I create it again. I researched and did not find anything about it, so I would like someone to give me an alternative to it. appreciate!

What I wanted was to be able to do when I pressed the button to return to her and when I reached the main house, I asked if I wanted to leave!

    
asked by anonymous 07.11.2014 / 18:02

2 answers

2

If I realize you do not need to be creating Activity main every time you get back there, just use the methods onRestoreInstanceState and onSaveInstanceState to pass the data from one end to the other without start and finish from 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);

}
    
07.11.2014 / 18:46
2

With Jorge's great help, I arrived at the following I rewrote the public void onBackPressed() function to return to the screen.

like this:

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 when I get to the main I execute 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!

    
11.11.2014 / 13:34