How to I delete a previous SavedInstanceState? - android

I'm making an Android app but I keep running into a odd bug. After the game, if you tap the 'Back' button on your phone, you end up back at the finished game and you can keep playing!!
How do I prevent this from happening? Is there a way to delete the previous SavedInstanceState? If so how?

If you really don't want to restore the data don't pass them to your super class. However I would prefer to reset the game data myself.
I guess the full solution is to call finish();. I guess you have this activity stack: Menu -> Game -> End summary. So when you start your "game over" activity add a finish() call to close your current game. So a back button press let the user go back to your menu.

Related

Minimize and restore activity / Fragement

I have a Voip calling app using kotlin and when call received, it will open an activity called IncomingView. So far so good. But I want to Minimize the activity on back button and can navigate to other pages and shows a green bar or indicator at the top bar (similar to WhatApp/Telegram) that indicates that the call is going on and when i tap there, it should bring back my "IncomingView". (I know and already implemented that i can create a notification and tap on it brings back my activity)
Any Ideas?
But I want to Minimize the activity on back button
you cant minimize Activity, you can close it. You can "minimize" a fragment in a activity by removing it and later on adding it back - this way its state will not be lost.
If you need to minimize an activity, then I suggest to save its state, then minimize it (call finish() on it), then when you need it back then open it again and it should read its old state and recreate in a same way as it was previously.

close app on home button & restart fragment

i have two simple questions (hope so).
First i want to close my app, when the user hits the homebutton in my HomeFragment, because there is a counter (Days,Hours) and when I click the home Button and come back later there is still the old value.
Then i have to close the app via backbutton and start again. So i want my app to get killed, when I hit the home button.
Then i have a button to delete the last value, on which the counter is based. When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
No, these functions are meant to be called by the system.
You are probably looking for the onStop() method (also called by the system)
protected void onStop()
{
super.onStop();
// clear counter e.g.
myCounter.setText("");
}
Other than this, it's kind of hard to tell what you want. Feel free to add more (if not all) of your code.

Reg back button behavior in android

I am having a specific problem in my android app. I have 2 activities and the flow is from
Activity 1 to Activity 2.
I press back button from Activity 2 and then it goes to activity 1. This is great. But in some devices if I stay too long on activity 2(Like for days, I check it once in a while ) , and when I press back button it doesn't go back. So I guess history is cleared by android os for getting more memory.
I know that I can manually override onBackPressed() and achieve the functionality, but I need a perfect solution for this. so it will be like
If the history is all right let android handle back, If history is disturbed then I should be able to handle it.
IS there any way to check any issue with history?

help calling an Intent / activity that has already been created

I have two buttons on my application. One button starts a new Game (lets say solitair).
When ever this button is pressed it will always start a new game
My problem is that I would like a second button to go back to the game that has already started if the user clicks back.
How do I go about recalling that activity that has already been created
Thanks
I assume that the game is from a third party. As I see it you don't even need second button. Just click the first one again and you will get back to the game.
If the game intercepts Back it may intentionally destroy it's state so there is no way to jump back to the place that you were before hitting Back.
You have to save the state of the activity
onSaveInstanceState() and onRestoreInstanceState() are used to handle the activity state
onSaveInstanceState() method gets called for an activity when user leaves the activity. Note this method is only called when activity is present in the History state and user can come back to the activity.
onRestoreInstanceState() restores the state of the views.
see the following link
How to manage activity state
You have to read manual about activity stack
Your application can has more than one task and more than one activity back stack

Different state storage behavior for "Back" vs "Home"

I am still learning the ins and outs of Android development. I am playing around with the Notepad tutorial application to try and get different behavior.
Right now, I want to have the application do the following in the NoteEdit activity:
1) If the Back button is pressed, current state is ignored; basically, it's like an implicit cancel, and you are taken back to the list.
2) If the Home button is pressed, it takes you to the home page as normal. However, if you open the application again, it should go back into the NoteEdit activity in the same state as when you left (IE, if you were partway through an edit, for example).
I removed the "saveState" stuff from onPause, because I don't want to store to the DB unless "Confirm" is pressed (instead, I moved the call to saveState to the confirm button). By doing this, hitting "Back" basically throws out your changes, which is what I want. However, going Home and coming back also throws out your changes, though it does remain in the NoteEdit activity. Both "Back" and "Home" cause the onPause message to trigger, and both cause onResume to trigger (either from clicking on the item in the "Back" case, or by going back into the app in the "Home" case).
Is there a way to have these two events handle saving the state differently? Is it possible to have the Home button store the state (temporarily), while not having the Back button do it?
Thanks in advance!
You need to define an onSaveInstanceState method, but instead of saving to the DB (as in the Notepad sample), save your Activity's state to the Bundle. You then need to recover from the saved state in your onCreate when the passed in Bundle is non-null.

Categories

Resources