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.
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'm trying to detect when the user click on BackPressed (this I can override the onBackPressed()) also the home button and the recent button.
My goal is, don't quit the app until the user Accepts a dialog, I mean, for instance I'm using an app, that doesn't let go to another app to see something, so I want to do something like a "Security thingy".
Scenario would be :
User is watching a video (that's an example)
User tries to press the square button to show recent apps
Then user sees a dialog saying if you leave you are gonna loose the video (whatever...) if he press yes do the normal stuff like see the recents if he presses no, don't show recents...
The same with the home button
I've already tried onPause() but the thing is that I see the dialog after the action is done and if I do an if condition it crashes because it needs the super.onPause().
Any clue how to achieve this?
I think you only can handle the back button. For back button, you can override the onBackPressed() method of your Activity and show the user a dialog and then call android.os.Process.killProcess(android.os.Process.myPid()) when the user clicks on Yes button of your dialog
#Override
public void onBackPressed() {
//show dialog
}
private void onDialogYesButtonClick() {
android.os.Process.killProcess(android.os.Process.myPid())
}
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.
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
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