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.
Related
I'm getting a bit confused here. I've got a navigation drawer with a Fragment in it for my google maps page. The idea is they click a marker, they see a button that takes them to a new activity.
What I want is for the user to see the maps page again if they exit the app via the home button, and refresh it, when they go back into the app. So is that possible?
Calling onStop(); in the activity does take me back to the fragment, but the map does not refresh. I call finish(); in the onStop(); so backstack wise the maps page is the most current.
Is there a method I'm missing that will let me refresh the map within it? I have a method that pulls the markers and what not when the map loads. But of course this just takes be back to where I was. I want to call the refreshMap(); method.
Maybe a cheaty way. I have a refresh button in the onCreateOptionsMenu so maybe just things like onKeyDown to force a back button, maybe we can force a tap on that button? Guess that's only for hardware buttons.
Hopefully someone can guide me the right direction. The refreshing is important because otherwise the user can click on an expired marker from hours ago and still get the offer.
Thanks for any help given, really appreciate it!
private static boolean homeClick = false;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
homeClick = true;
}
return super.onKeyDown(keyCode, event);
}
Check if homeClick has clicked, if so then we finish the current activity and go back to the last
Make sure the first activity (the one with the map is still active, if not make an intent before finish()
#Override
protected void onResume() {
if (homeClick) {
homeClick = false;
finish();
}
super.onResume();
}
I think with the way onResume() works on the Maps page I should not refresh it. An just do a check before they go to activity, to make sure it has not expired. I should just go back to the page and tell them it has expired.
It only refreshs the map if the map is null. If I change that you will be stuck on a loop when using the app again, since onResume it called.
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 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
.
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 have an activity that launches the Contact picker in the onCreate method. When the user selects a contact, that user's ID gets passed to a new child activity. If the user presses the back key however, they get presented with a blank screen cause the onCreate doesn't seem to be getting called again.
I've tried, onRestart and onResume, which fix the back issue, but what happens it a race condition in which it will make the call to open up an Contact picker activity before the ID is passed to the child activity.
EDIT:
I'm using API 2.3.3.
I've tried the overloading the onBack method in the child activity to launch a new activity. But doing changes how the rendering of the page changes is done. When you launch a new activity the new activity comes in from the right hand side of the screen, but when you hit back the normal implementation causes the previous screen to slide in from the left-hand side. If I overload the onBack to call a new activity, what happens is the user hits the back button and a screen comes in from the right hand side of the screen as opposed to the left. I would need to have the window sliding be the same as the default pattern.
You cannot call onCreate on back key pressed. However you can do one thing that finish your contact picker activity. Override the function onBackPressed in Child activity and start a new Activity there.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(false);//this will not allow to go back
//Here put your code i.e start new activity but first finish current activity
return true;
}
return super.onKeyDown(keyCode, event);
}