开发者

Android: How to simulate back button

开发者 https://www.devze.com 2023-03-25 13:59 出处:网络
Currently my activity allows users to fill up certain data. Including spinner etc. When user click next system navigates to another s开发者_运维知识库creen. When I press back button on the phone previ

Currently my activity allows users to fill up certain data. Including spinner etc. When user click next system navigates to another s开发者_运维知识库creen. 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.


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.


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();
 }
}


As said earlier, just finish your activity:

myButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        finish();
    }
});


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.


In your soft Back button onClick event write

Intent _intent = new Intent(PresentActivity.this,PreviousActivity.class);
startActivity(_intent);
0

精彩评论

暂无评论...
验证码 换一张
取 消