Reset Activity when phones back button is clicked - java

Is it possible to reset a activity to its normal state when a user clicks the back button from the previous page.
Use Case: On my main activity I have checkboxes and the user goes to the next activity using intent. That activity has a "Back to Main Page" button, but when the user just clicks the back button on the phone the checkboxes are already checked from the last time the user was on the page.

If you want to reset only when returning from that activity then use startActivityForResult() and override onActivityResult.
If you want to reset everytime then reset in onResume()

Move the code that sets the default state of the checkboxes from the onCreate() to onResume() method.

Related

How to enable back button of phone to go back to previous activity without enabling back button icon on action bar?

Suppose I move to another activity from my current one and then I need to go back to previous activity by pressing the back button originally present in the phone along with a home button and menu button. I have disabled my Action Bar and I don't want to enable it. Nor I want to create an icon in my activity that represents back.
Please Help!
to initiate the back operation you can call
super.onBackPressed();
from your activity.

multiple fragments and activity all responding to button click in activity

I have a multi-fragment form dividing sections of a form. When the user is finished they hit the submit button at the bottom of the screen which is owned by the hosting activity. When the user clicks submit all the fragments should send their data to the activity and then the activity handles the networking. So I have onClick listeners set for the same button in every fragment, as well as the activity. However when I hit submit onClick only runs once, in some fragment (never in the activity), when it should run 5 times (because I have 4 fragments and 1 activity). Basically I need the fragments to deal with the click and then the activity deal with the same click.
Use the Observer design pattern where the activity is the Subject and the fragments are the Observers

Saving changes in Activity after going to another Activity

So I have a Button in FirstActivity.java which when you press leads you to a second activity: SecondActivity.java, which has another button BackButton that leads you back to FirstActivity.java. The code in the first activity allows you to change the layout according to different conditions. Is there a way that if the layout of FirstActivity.java is changed, you press the Button to go to the second activity, then when you press the BackButton to go back to FirstActivity.java, the layout changes will be saved in the first activity?
You can use putExtra for send data to SecondActivity, and return data to onActivityResult.

How to navigate back to my app instead of going down the backstack?

Let's say the user presses a button which causes another activity to get launched. Now the user is in another activity. What flag do I have to add to the intent, so that the user returns to my app when pressing the back button instead of navigating down the back stack of the started activity?
From the documentation, "activities on the backstack are never rearranged."
Android documentation Tasks and BackStack
You do not have to add anything to the intent. When you navigate out of you app and start another activity to complete a task, the new activity becomes kind of like an extension of your app. Pressing the back button takes you back to your app. Read This

onActivityResult() never called when Home is used instead of Back

I've got an app that launches another activity via startActivityForResult. Everything works great if the user exits the invoked Activity via back button, however if the user presses Home instead, the invoked Activity exits and onActivityResult never gets called.
Is this expected behavior or should this work?
Difference Between Pressing Home Button and Back Button.
By default, pressing the Back button finishes (destroys) the current activity and displays the previous activity to the user.
By Default , pressing the Home Button activity is stopped and moved to the background rather than being destroyed
More in Details - Read Navigating Away from an Activity with Back and Home buttons

Categories