Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to simulate back button

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.

like image 988
Jay Mayu Avatar asked Aug 01 '11 11:08

Jay Mayu


1 Answers

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.

like image 154
Ovidiu Latcu Avatar answered Sep 19 '22 12:09

Ovidiu Latcu