I am working on the application where I am using the TabActivity.
Now what the problem I am facing is Suppose I have two Screens say Screen A and Screen B.
Now when I am in Screen A and I enter the Data in the EditBox of it and then I switch to the next Screen i.e. Screen B and then I enter some data in the EditBox of the Screen B also.
Now when I get back to the Screen A, I can view the Data that I had entered in the EditBox of Screen A(Which I can see), but when I again navigate to the Screen B, I need to see the Data that I had entered in the Screen B before coming back to Screen A.
I am not able to sustain the Data in the Screen B once I come back to Screen A and then again coming back to Screen B.
I know the question could be good enough to chew your mind, but the Issue is doing exactly the same for me.
Use Shared Preferences to store your data. When you navigate from one activity to another activity. It may result in DATA loss
So use Shared Preferences
Related
I have a simple navigation flow:
Screen A -> Screen B
After I do some work in Screen A, I navigate to Screen B, and do some work there. Then I remember I forgot to finish a task in Screen A, and I press the system back button and it takes me back to A. I can see all the local states are preserved/remembered (texts in textfield, counts, etc). As I finish my task, I want to return to Screen B, but it is already popped off of the stack. So I manually navigate to Screen B, and with no surprises, the local states are gone.
My goal is, as I can navigate backwards easily, I want to navigate forward in same manner (states preserved). Any help will be appreciated. Thanks.
Navigation isn't really designed to work like that, but you can work around it by manually saving the states yourself. You can save specific data in onSaveInstanceState() and restore it in onRestoreInstanceState(), however there are some cases when onRestoreInstanceState() isn't called when the activity is destroyed.
You're probably better of manually saving your state in SharedPreferences in onPause(), and restore them in onCreate()
I would like to save state of an activity when you navigate away from it. For example, you have Activity A with filled in text on it and you navigate to Activity B using an intent. Well when you go back to Activity A using an Intent you lose all the text you had on that screen.
When I use the soft key back button on most android phones, the screens text stays on there, I would like to mimic that method of keeping the text on screen between navigation.
The last thing I want to do is pass parameters between all my screens, there are a lot of forms and the users dont have to follow a specific nav order.
I am following the tutorial here:
http://developer.android.com/training/basics/firstapp/starting-activity.html
When I enter some text into the field and press Send, it goes to the second screen with the larger text, so that part works.
When I click the "back" button on my phone, the text I inputted is still in the text field. But if I press the back button on the screen near the top of the program, the text I inputted is gone.
Why is this and how can I control / change this?
Reason for your issue - Your app creates a new instance of activity when you click the back button in your app.
To fix this
In the back button where you call your first activity, add this line of code
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
What happens with this flag ?
When you use this flag, Android looks for an instance of the desired activity in your activity stack, starting from the front of the stack and scanning until it gets to the root/back of the stack. As soon as it finds an instance of the specified activity, it brings that one to the front (ie: if there are multiple instances of the specified activity it will bring to the front the most recent instance).
I'm implementing a not-so-standard navigational menu that is accessible from every screen in my app. That is to say, from every screen in my app, I can pop up a menu that will let me choose an entirely different area of the app to navigate directly to.
I have several screens that I call edit screens. They are screens where the user had chosen an item from a list of items and are able to then edit the data for that item. I do not wish for these screens to remain on the Activity stack if the user then uses the menu to navigate to some other area of the app.
This is easy enough. I can simply call "finish()" before navigating away. However, there are a couple places where it is possible to access a nested edit screen. Meaning the edit screen the user is currently on is a child of a parent edit screen. I want both off the Activity stack.
Can anyone think of a slick way to do this? The only way I can think of is to always use startActivityForResult and pass back some identifier that tells the screen to kill itself the next time it resumes.
I have 3 screens(xmls):
screen 1 is main menu,
screen 2 is main game and
screen 3 is showing either game over screen or game finished, screen 3's background is blurred transparent on top of screen 2.
All these navigations are creating new activity(view) and using intent to go next screen.
problem1 : if i keep on creating new activities whenever user go to screen 1 to screen 2 and finishes the game and screen 3 will have option to new game which will create new activity of screen 2, in this case, stack will be more and to go out of the game user should press back button in more number (stack numbers). I would like to know though game creates multiple activities, in stack only 3 screens should retain (or when user clicks on back button 3 times, game should come out)
problem2: user clicks on backbutton when blurred screen 3 on top of screen2 should go to main screen (screen1), as per current stage, when i click on back button its going to screen2 (from blurred screen3, which is no meaning for me)
Thanks
Problem 1 : write android:launchMode="singleTop" for each activity tag in manifest . this will avoid creation of multiple instances into history stack , will keep latest entry only .
Problem 2 write android:noHistory="true" in Screen2 activity tag of manifest,so that there will be no entry for Screen2 in history stack and back press on screen3 will show screen 1 . also have a look of Intent.FLAG_ACTIVITY_NO_HISTORY
What you want to do is set the launch mode for these activites to single top in your manifest. In your manifest set android:launchMode=singleTop for each activity you only want one instance of. For more information, checkout this.