My application settings can be opened and closed with hardware menu button (as well as with back button when navigating out of the root screen). There are many child screens in this preferences.
(And lets say, there are two activities - main and settings).
I'd like that I could return to the last preference screen I was working with when I access application settings again.
Saving last preferenceScreen key and using setPreferenceScreen(lastPreferenceScreenKey) is not relevant as in this case I can't navigate up the hierarchy.
Setting PreferenceActivity.launchMode="singleTask" and starting main activity when user presses menu button didn't help - when I starting settings activity again Its root screen appears.
Is it possible somehow or another?
Maybe try to use a back button pressed listener:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// Back button pressed
}
return super.onKeyDown(keyCode, event);
}
Try to use sharedPreference for storing last visited screen and use a dispatcher activity to dispatch to the correct activity based on the preference on launching the application.
Refer this link for your reference
.
Related
In my application, There is an activity UserMenu, in which (at on create) subsequent loading is done. I want that next time when the application is opened, this screen should not be reloaded again - I want to save the state of the UserMenu screen as it is.
I have been moving through this, but I am unable to look what I have to implement, I dont want to save any values, simply I want to recreate the same screen as I can get on pressing the home button in android.
Kindly help me regarding my matter.
Thanks
You can do like this
In your UserMenu Activity class
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
goToHome();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void goToHome() {
moveTaskToBack(true);
}
So when you will press back button the Activity will no be destroyed and move to Home and once you will long press Home Button or start your app from menu it wont load the Activity again.
Thanks
It is not possible according to me as like what you are saying about home button. when home button is pressed your activity goes in onPause() and when you reopen it comes in onResume(). You can do what is suggested in the link you are following i.e you can save the values of your Actvity elements using onSavedInstanceState() also it is no guaranteed to execute read this onSavedInstanceState
you need to follow the Activity Lifecycle to make it simple.just make a function call from onCreate() method to load those data,save the data in some standalone class using ArrayList(),and in onResume() display the stored data from ArrayList().and yes,inside onCreate() check for the size of that ArrayList() just to decide whether the data has already been loaded or not.if it has not been loaded,load it.otherwise don't load.
In my game, if a user hits the back button I pop up a dialog asking if they really want to quit. However, I can't do the same with the home button because there's no way to override it.
If the user knows the task manager trick they can hold down home and switch back to the app and not lose their place.
If they don't know the trick they'll just select the icon again which will start the application over from the main menu.
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
I know that I could save the state of everything when the app pauses and then programmatically reload everything and send them to where they were. I'd like to avoid having to do all that work if possible.
However, I can't do the same with the home button because there's no way to override it.
You can,actually,override the home button.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
//do your stuff
return true;
}
return super.onKeyDown(keyCode, event);
}
Is there any way to override this behavior so that instead of starting at the main menu it would go back to where it was if the app is currently running?
About this, your best bet is to override the onPause() method in every Activity. But there is no guarantee about it, because, the OS might choose to kill your application when the user navigates away from it(for resource requirements,maybe). So in such a case, your application will start off from the main menu, and you can't help it.
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!
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.
I having problem in my application.. I am pressing back button in my third activity but its running in running in background(i noticed by used Log statement). If again load application its starts from first activity instead of third activity. please suggest some idea to do this..
This is my back button coding
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
}
return true;
}
Thanks in advance..
The back button pauses your current activity and pop it from the activity stack, so the previous activity will be shown. You don't have and in most cases you just shouldn't override this behaviour.
Basicly, if you're in your third activity and you press the home button, then relaunch the application the third activtiy will be shown.
Please, refer to Activity lifecycle.
When you are in your third activity and then press home button. The next time if you want your activity to start from the third activity, Long press the home button and then invoke the application from this list rather than launching it from launcher activity.