6

Currently my activity allows users to fill up certain data. Including spinner etc. When user click next system navigates to another screen. When I press back button on the phone previous activity loaded and filled data is available.

My requirement asks me to give a soft "Back" button in the UI. When user clicks it it navigats back to previous screen but the filled data is not available.

Is there any way to simulate the back button on a soft UI button onclick event??

Intent back = new Intent(ImagePreviewActivity.this, sendingpage.class);
startActivity(back);

Thanks in advance for your time.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
  • Possible duplicate of [Android - Simulate Back Button](http://stackoverflow.com/questions/2717954/android-simulate-back-button) – blahdiblah Jan 04 '13 at 00:48

5 Answers5

15

You'll just have to call your Activity finish() method when the user clicks the soft back button.

EDIT: just let your Activity implement OnClickListener and then in code

          myBackButton.setOnClickListener(this);
   ....

   public void onClick(View v) {
       if(v.getId()==R.id.YourBackButton){
           finish();
       }
   }

EDIT2: you can also call onBackPressed() from your Activity.

Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • This does not simulate the back button it simply terminates the activity. Consider, for example trying to use this in multipage preferences screen. It would simply terminate your preference activity and would not return to the previos page. – Fuzzy Nov 17 '12 at 11:40
3

If you are using Activities to show different screen then simply finish the activity with some result based on a button click and you can pass some Activity Result value back which can then be handled in onActivityResult of previous activity.

Adding some pseudo code. Assuming you have two Activty A and B and you go from A -> B and then from B -> A

in Activity A

 startActivityforResult(new Intent(A.this, B.class), 1234);

 onActivityResult(......) {
    if (1234 == requestCode) {
        switch (resultCode) {
             /* Do your processing here like clear up old values and so on */
        }
    }
 }

in Activity B

onClick() {

  if (v == backBtn) {
           Intent resultIntent = new Intent();
    setResult(Activity.RESULT_OK, resultIntent);
    finish();
 }
}
PravinCG
  • 7,688
  • 3
  • 30
  • 55
1

All solutions on this post are too complex. The most simple is on this post:

Android - Simulate Back Button

It's the last solution on the post:

this.onBackPressed();

OR

getActivity().onBackPressed();

Dependely from where you are calling it.

Community
  • 1
  • 1
Pedro Aguayo
  • 109
  • 1
  • 4
1

As said earlier, just finish your activity:

myButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        finish();
    }
});
nhaarman
  • 98,571
  • 55
  • 246
  • 278
-1

In your soft Back button onClick event write

Intent _intent = new Intent(PresentActivity.this,PreviousActivity.class);
startActivity(_intent);
Rasel
  • 15,499
  • 6
  • 40
  • 50