Android Home Button disable back - android

if a user tapped the home button and open the app after that, how to not allow back? eg don't allow users to go back to screens that they seen before tapping the home button. It should be treated as a new session

This sounds like a bad idea, as it blatantly goes against the Android task/navigation guidelines.
The user expects to be able to back out to the previous screen after resuming a task... and preventing it will potentially piss off a lot of users.
Please, please, please read these documents before you risk destroying the user experience.
App structure
Navigation
Tasks and back stack

The home button cannot be overridden nore should it, if you dont want the user to go back to the activity they left when the home button was clicked then on the on pause of the activity just pop the backstack to where you want to be.
see this answer

If you want to end your Activity once it is no longer visible then finish your activity in your Activities call to onStop().
#Override
protected void onStop() {
super.onStop();
this.finish();
}
This will finish your Activity with no chance of onRestart() being called. Be careful with this method because users expect to resume the app instead of having to start over, but there are cases where this would be the accepted protocol. See more on Navigation and the Activity LifeCycle.
Edit:
Also see the question Android-Quittings an application - is that frowned upon? specifically this answer.

Related

Why does "Home" button call `onDestroy()` in my Android app?

On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.

Pro and cons calling finish

I want to know what are the pro and cons when you try to open a new activity with android and destroy the previous one straight away by calling finish.
People think that is a bad idea because Android can take care of the activity and drop them when there is too much memory used, but what about if I get inside that activity once and probably the user will never come back? Is this a bad option?
Also by finishing the activity, the history with the back button is "clear", so it wont get back to that activity ( only if your user flow needed to go back I think you should not call finish ).
And in terms of memory, is better to kill the activity with finish or leave android to have this activity in the background for who knows for how long time?
I feel like, that you kind of help the system to GC the activity that you closed and make sure that the user wont need to tap the back button 100 times before getting out of the application.
So what do you think? Better call Finish or not
I want to know what are the pro and cons when you try to open a new activity with android and destroy the previous one straight away by calling finish.
Either you want the user to return to the previous activity via the BACK button, or you do not.
If you want the user to return to the previous activity via BACK, do not call finish()
If you do not want the user to return to the previous activity via BACK, there are a multitude of options, depending upon where you do want the user to go when the user presses BACK
People think that is a bad idea because Android can take care of the activity and drop them when there is too much memory used
No, Android does not do this.
is better to kill the activity with finish or leave android to have this activity in the background for who knows for how long time?
It is "better" to have the activity implement onTrimMemory() and reduce its memory footprint as needed. Do not harm the user expectations of the BACK button.
that you kind of help the system to GC the activity that you closed and make sure that the user wont need to tap the back button 100 times before getting out of the application
Few users will "tap the back button 100 times". They will press HOME, or bring up the overview screen (a.k.a., recent-tasks list), or navigate to another app by other means.
Now, that being said, there will be times when you want to clear the task (back stack), again with an eye towards providing a logical flow for the user. For example, in an email app:
The user launches the app, and a fresh task is created, with the user going to the app's launcher activity, which shows the messages in the user's inbox (A)
The user taps on a "search" action bar item, bringing up a search activity, where they can search by various criteria (B)
The user fills in search criteria and clicks the "Go!" button, which does the search and shows matching email messages (C)
The user taps on an email message, bringing up an email-viewing activity (D)
The user taps a "delete" action bar item, which should delete the message and return the user... somewhere
If you believe that the user should return to the search results (C), you could call finish() in D. If, however, you believe that the user should return to the inbox (A), you would call startActivity() on A with appropriate flags (e.g., Intent.FLAG_ACTIVITY_CLEAR_TASK), to clear out the back stack and return the user to A.
In sum: do NOT call finish() to deal with heap space; implement onTrimMemory() instead. However, if navigation calls for finish(), then use it.

How to complete the application when pressed buttons Home i Back?

How to complete the application when pressed buttons Home and Back? Provided that in my memory holds many pictures.
When pressed on the Back button - application restarts... When pressed on the Home button - quit application, but when it restart - does not start Splashstsreen.
Hard to see without code but it sounds like your activity is resuming rather then starting from scratch (as it should behave). It sounds like it's behaving correctly in that case. If you insist in wanting your application to truly quit after the back button is clicked perhaps you can override onBackPressed() then have your Activity call its finish() method.
I don't think it's good programming practice to fiddle and interfering with the activities lifecycle. It's the OS responsibility to manage the lifecycle, including pause and finish activities.
Instead you should use other methods to handle your problem with not showing splash screen, these methods are onResume and maybe also onStart(). Also you should get familiar with the activity lifecycle(link submitted by #ss1271).
Press Home will let the activity to enter onPause(). however, if you insist to quit the application when press HOME, which is obviously not a good practise, you can do like this:
Override onPause() and onBackPressed()
add finish(); to these methods.
I am sure by adding finish(); to onBackPressed() will quit the app when Back button pressed, but I haven't tried on Home.
Please refer to Activity Lifecycle for more info.

Is Home Activity instantiated multiple times?

The official Dev Guide of Tasks and Back Stack says, activities can be instantiated multiple times, and Home Activity is taken as an example
So I tried it out as the graph illustrates:
Launch Activity 2
Press Home button
Launch Activity 1
Press Back button (so I return to Home screen)
Press Back button again
But I did not go back to Activity 1. Thus, it seems that Home Activity has not been instantiated multiple times. Is it so? If so, how is it kept in a Back Stack?
EDIT: Sorry, I should've clarified earlier that I didn't write any codes to test it. All I've done is just launching applications on favorites tray. I'd better go to read the source code and search for the behavior of Home Activity.
Anyway, I don't think Home Activity is a good example here to illustrate multiple instances.
Your issue might be that you might have called finish() in your Activity2. Or, the OS clears up the 2nd Activity before you return back to it. The behavior you are trying to attain on your own has no guarantees. You can't force an Activity to keep running so that you can return back to it.

Which actions does the back button/back key on Android trigger?

I am really confused. I have read that the back button
calls onDestroy()
can close up your currently-running activity
calls onPause()
I think onPause() should be right. But this is an side effect, because the Activity gets into the background. I found nothing in the docs. But maybe I have overlooked something.
Can someone please explain to me what the back button is supposed to do programmatically? Some references would also be nice. :-)
I have read that the back button calls onDestroy(), can close up your currently-running activity, calls onPause()
All three are correct.
I found nothing in the docs.
Quoting the Android documentation:
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
To elaborate, if there is nothing else that will consume the BACK button press (e.g., an open options menu), your Activity will be called with onBackPressed(). The default implementation of this calls finish(). This will take your activity from the running to the destroyed states, calling onPause(), onStop(), and onDestroy() in sequence, as shown in the event flow diagram:
Just to add, browser application overrides onBackPressed() to go back to previously opened tabs (if available) and it not, closes the application.

Categories

Resources