Android: how to simulate a return button

Currently, my activity allows users to fill out certain data. Including spinner, etc. When the user clicks the next system, he goes to another screen. When I press the back button on the phone, the previous activity is downloaded and the data is filled.

My requirement asks me to give a soft back button in the user interface. When the user clicks on it, he goes to the previous screen, but the filled data is not available.

Is there any way to simulate the back button on the soft button of the onclick event user interface?

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

Thanks in advance for your time.

+6
source share
5 answers

You just need to call the Activity finish() method when the user clicks the soft feedback 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 .

+14
source

If you use “Actions” to display another screen, just perform an operation with some result based on the click of a button, and you can pass some value to the result “Result”, which can then be processed into the onActivityResult of the previous activity.

Adding some pseudo code. Assuming you have two Activations A and B, and you go from A → B, and then from B → A

in action 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 action B

 onClick() { if (v == backBtn) { Intent resultIntent = new Intent(); setResult(Activity.RESULT_OK, resultIntent); finish(); } } 
+3
source

As already mentioned, just end your activity:

 myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); 
+1
source

All the decisions in this post are too complicated. The simplest thing about this post:

Android - Imitation back button

This is the last solution in the post:

this.onBackPressed ();

OR

getActivity () onBackPressed () ;.

Depending on where you call it.

+1
source

In your soft back button onClick event write

 Intent _intent = new Intent(PresentActivity.this,PreviousActivity.class); startActivity(_intent); 
-1
source

Source: https://habr.com/ru/post/894023/


All Articles