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
Related
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.
I am working on a tracking application that use another third party application such as google map.
Lets assume my application has 4 activities ( act1, act2 , act3 and act4 ).
on act3 activity i am calling google map application to calculate the distance and view the routing. But once i clicked on the back button of the tablet, or go back to the home page and clicked on the launched icon, the act1 is displaying instead of act3.
However, my application is still running on background.
So my question here how can i make the application is opened the last activity which is act3 in our example, when the back button is pressed or when the launched icon is clicked.
Thanks,
Store the name of the running activity into shared preferences in the onPause of every activity (if you're exiting by pressing home button) or in onBackPressed (if exiting an activity by pressing back button).
Then create a launcher activity which reads the shared preferences and launches the specific activity.
Lastly remove the launcher activity from the activity stack so that the app doesn't go back to it on pressing back button.
I am working on a small app. When the user presses the app icon it starts activity A, which in turns starts activity B. Activity A then completes. B is set up as a main menu and can start other activities: the user can navigate back to B with the back button.
If the user navigates back to B and presses the back button the app moves into the background and the user is at their home screen. I have not overridden anything; this is the normal navigation.
At this point, if the user presses the app icon the app restarts. I understand that when the app is in the background the OS can close it for memory purposes, but this happens every time - regardless of how much memory. Is there a way to change this behavior? I already figured out how to stop this action with the home button with:
if (!isTaskRoot())
But I need to stop the action on the back button.
You can override onBackPressed() of Activity B like this,
#Override
public void onBackPressed () {
moveTaskToBack(true);
}
The app will be hidden when the user presses the back button, but it's state will remain the same. And when it is reopened, it will appear just as it was when you left it.
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
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.