1

I'm developing an application in android, where i'm looking for a solution.

There is an Activity(say A1) from which by clicking a button, user goes to another Activity(say A2). Now once the user has finished with A2 activity, he clicks back-button, to go back to previous activity A1. Well this is a very known fact that, A1 will resume automatically at this point.

But I want it in a different way (because my application demands that).

Once if i go to activity A2 from A1, A1 is destroyed; and while being in A2 if back-button is clicked, A1 is created again.

Can anyone tell me how to make this work?

kishoredbn
  • 2,007
  • 4
  • 28
  • 47
  • 1
    What's the special point that A1 must be destroyed and be re-created? If you have some processes that are done during onCreate() in your case, I think you could probably move most of your codes in onCreate() to onResume(). – Victor Wong Feb 13 '12 at 06:41
  • Please check http://stackoverflow.com/questions/4778754/kill-activity-on-back-button which may help. Thanks, Developerjigar – Jigar Pandya Feb 13 '12 at 06:41
  • @VictorWong~~ tried that already, didn't worked. but it seems like i need to re-create it. – kishoredbn Feb 13 '12 at 06:49

3 Answers3

13

you can use:

public void onBackPressed()  
{  
    //do whatever you want the 'Back' button to do  
    //as an example the 'Back' button is set to start a new Activity named 'NewActivity'  
    this.startActivity(new Intent(YourActivity.this,NewActivity.class));  

    return;  
}  

look at here: http://www.41post.com/1685/programming/android-changing-the-back-button-behaviour

Behnam Safari
  • 2,941
  • 6
  • 27
  • 42
1

You can override the Back Button key press like so:

@Override
public boolean onKeyUp(int keyCode, KeyEvent msg) {

     switch(keyCode) {
     case(KeyEvent.KEYCODE_BACK):
          Intent a1_intent = new Intent(this, A1Activity.class);
          startActivity(a1_intent);
          finish();
          return true;



     }
     return false;
}

Take a look at this

-4

Try this:

public void onBackPressed() {  
    finish();
    return;  
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Nitesh Khosla
  • 875
  • 8
  • 20