I have Main Activity. That has 4 tabs(TabHost). I have overridden onBackPress() in MainActvity , as well As All 4 activities. This button show user a dialog box and for conformation of Exit
When app start. It show 1st tab. Then if I press back it work fine. But if I go for next 3 tab and then press back, The app stop. OnDestroy() of Main is called. But there is not dialogue for the user.Even noting is print in log cat. That I have written in onBackPressed() method From and of 5 activities including MainActivity.
I have also try onKeyDown() for back key but result is same? Have any one experience the same? Please help me.
I come to know it was difficult to open new activity inside the previous tabs when I am using TabHost. I google it and found GroupActivity is the best solution for this problem.
GroupActvity Example
But GroupActivity have the same problem when open new activity in the previous tab. the back button not work properly for new activity. After search I found that was due to the focus was alwasys on parent activity. I have to make
setFocusable(true);
requestFocus();
on my new activity component to gain focus.
I am now using GroupActivity for customizing Tabbar and activities.As I ma also maintaining stack of activity ids in parent activity so that I can pop out the recent activity when user press back button.
else if you are NOT going to implement Activity focus then you should maintain stack in parent and when press the back button it will initiate parent onBackPressed(); and you can call the child onBackPressed() functionality as discussed in the link.
onBackPressed() not working inside ActivityGroup
well one thing i know for sure is that you will always get the onBackPressed() called in the MainActity if you are running with a tabHost and not in the child views. The only thing that comes to mind is if you have consumed the event in the onBackPressed method (return true) because if you didnt it will go and still follow the default process and destroy your activity.
I meet this problem,but i have shot it now.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
showExitDialog();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void showExitDialog()
{
new AlertDialog.Builder(this)
.setTitle("Attention")
.setMessage("Do you want to exit this application")
.setPositiveButton("YES", exitListener)
.setNegativeButton("No", cancelListener)
.show();
}
at the first time i lost a "reture true" in onkeydown()
Related
I have an app that allows user to select a txt file from a list and then goes off to the internet to get the contents of that file. All works well except when the user accidentally or deliberately presses the hardware back button to go and see the list again.
Now, when the user clicks a new item from the list (a new file that is), instead of loading a the new file, the app continues off from where it was suspended.I do not want that to happen. I know this is related to the life cycle of the activity.
How do I make sure that it loads the new file rather than continuing from where it left off ?
I suppose you're loading the file in onCreate(). You should do that in onResume() instead.
Do not force Activities to close (e.g. use finish()). First, this does not guarantee the Activity will be closed, and second, this is better left to Android to decide.
Another Method to kill an activity is by calling following method.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
You can override the onBackPressed method in the activity and call finish() to get the desired outcome.
You just need to finish your activity in onBackPressed() method by calling activity.finish();
To destroy activity on back press, use this code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Destroys activity.
finish();
}
My app has three main activity.
Splash Activity
Login Activity
Menu Activity
When I start app, splash screen work with four second, then it goes login activity. If user login successfully then app goes menu activity. In my menu, there is logout button. If user clicks it, then app goes login activity.It works fine. But if users don't want to logout and click back button on devices, I want to exit from app directly. If I couldn't find solution for this problem, users have to go login screen, then splash screen and finally exit when they want to exit from app with using back button. Which solution should I use?
After the login has been successful you can call finish() on your Login Activity just after you started your Menu Activity. This effectively removes the Login Activity from the back stack.
Edit: As mentioned in the comment below, this also applies to your Splash Activity.
The solution provided by #Keyboardsurfer will work extremely good for you, but if you want to handle Back button event then use below code to get notified about back button event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
// your code to handle back button event.
return true;
}
return super.onKeyDown(keyCode, event);
}
When the login is successful you should call your Menu Activity as follows:
Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
I think you are checking in your Splash Activity if the user is logged via SharedPreferences, you should close your Splash Activity in the same way that you specify when you call any of the two activities.
You have to override the Activity's (Splash only) onPause method:
Here is how;
#Override
protected void onPause() {
super.onPause();
this.finish();
}
and in login activity call the finish method after the login stuff is done. Answer Edited (See the discussion below)
I don't think it would cause unexpected behaviour in other Activities, only the Splash Activity cause its its onPause() that was override not the others, and since splash is only called once i don't think it matters.
I am developing an application which starts from LoginPage. When user Login then he moves to Main Screen where grid view for different departments are present.
Every page of application except login page has a Footer which have different Icons like Home, logout, etc.
I want to add conditional back functionality using mobile back button. Some conditions are as follow:
1) LoginPage ---> Main Screen ---> On back user should log out and go to Login Page
2) Main Screen --> any department ---> Any Sub deprtment --> If user press Back button then go to back in same order
3) User is any where in application ---> If press home button from Footer ---> Comes to Main Screen --> No back functioality to go on previous page, It should follow condition 1.
4) If User on Login Page then he will exit from application on pressing Back Button
5) If User on main Screen then user should logout and go to Login Page on preseeing Back Button
I have tried with "noHistory=true" in Manifest and with Intent flags in Activity file.
Can any body suggest me best way to solved out it.
shouldn't be a problem, all you have to do is override the onBack function and add the logout process.
not a problem, the normal behavior of back buttons is exactly that.
DO NOT DO THIS!!! BAD BEHAVIOR.
normal behavior of back button.
that was step one.
this is used for exit from application on back press.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
if u want only back then remove System.exit(0) from above code .
By using this you can manage your all condition which one you want.
Use a stack globally to save screens order. Stack must be available in application level. And get the screen order when you click on back button. Write switch case for screen order and start that activity. that's it.
for example.
crate a class class MyStack{
//here declare a static stack
//create setter,getter method for assinging values to stack
}
when starting new activity assing screen value in stack with setter method
if you are starting a activity from main screen assign 1 into stack, you are starting sub screen assign 2 into stack.
when click on back get that value
switch(value){
case 1: //start mainscreen break;
case 2: //start sub screen break;
}
With what I understand, you cannot override the functionality of home button. By default, it minimizes your app with its current state, by calling onPause(). When you open the app again, onResume() is called and starts the app from where it was paused. As far as your back button functionality is concerned, most of the above answers are fine.
Try,
#Override
public void onBackPressed()
{
finish(); //finishes the current activity and doesnt save in stock
Intent i = new Intent(CurrentActivity.this, Login.class);
i.addflags(Intent.flag_activity_no_history);
startActivity(i);
}
Try this to trap events on the back button
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK) {
Intent Act2Intent = new Intent(thisActivity, Activity2.class);
startActivity(Act2Intent);
finish();
return true;
}
return false;
}
on each activity implement
OnBackPress().
Override it and add the functionality you want like logging out, clearing history stack and start new(previous) activity.
I think simplest approach may be to override back button in your "Main Screen" activity so that when back button is pressed you can do :
1. Executing log out logic:
2. Explicitly call your Login Page
This may give the behavior you are looking for.
On how to override back button, you can refer to this link:
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Hope this helps!
I have some strange effects in my Android App.
I use a TabHoster with TabGroupActivities for each Tab. Works pretty good except the behaviour of the Back Button.
An Activity is launched and responds to the Back button. Then I start a Child Activity with a ViewSwitcher. When i hit the Back Button on View A Activity is dismissed as Expected. But it gets through the onBackPressed() of my Activity.
When i hit the Back Button on View B (detailView==true) the Method is not even Called. Instead of that the onBackPressed() of the TabGroupActivity is Called and I am not able to switch back to the first View.
#Override
public void onBackPressed() {
if (detailView == true){
vf.showPrevious();
detailView = false;
}
else {
super.onBackPressed();
}
return;
}
Can Anybody explain this and/or tell me how to switch between Views in an Activity in an ActivityGroup?
Few Days later i came to the Conclusion that it is FOR MY CASE the easiest solution to throw away the ViewSwitching stuff and transfer them to seperate Activities.
Sebastian Olsson is surely right with the fragments but it would be more effort for my specific App to rebuild everything to fragments.
A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it takes me right out of the tabs activity so I have to click a few times to get back to where I was.
Is there a way to set the back button to go to a specific activity rather than killing the current activity window?
I believe you should be able to do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start new Activity here
}
return super.onKeyDown(keyCode, event);
}
But overriding the expected functionality of the back button is not advisable.
In general, I would advise against that because it breaks the UX. The user expects the back button to kill the entire window, especially since you are using the tabhost. To the user, the entire bunch (tabs and all) is a single activity that he wants to exit when he hits the back button.
If you still want to do it, refer to #onBackPressed(). It is called when the activity has detected the user's press of the back key. The default is to finish the activity, but you can make it do whatever you want. I advise care and caution.
You might find some inspiration from here.