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.
Related
I have two fragments (for ex. fragmentA and fragmentB).
first , in fragmentA use findNavController().navigate(R.id.action_fragmentA_to_fragmentB) to navigate to fragmentB .
then , in fragmentB if you want to back to fragmentA. there are two ways in below :
just press back button : fragmentA's onCreate() won't be called
findNavController().navigate(R.id.action_fragmentB_to_fragmentA) : fragmentA's onCreate() will be called
why?
The reason that the back button doesn't call onCreate of the fragment is by design. Users would not expect the back button to call the onCreate, or create, your fragment again.
As an example, think about when you open the YouTube app on Android and you are shown your home screen, populated with videos based on your interests. When you tap on a video after a bit of scrolling and then midway through the video decide to go back by pressing the back button, you expect the app to go back where you tapped on the video, with the same amount of scrolling you had done, instead of reloading your entire home page again filling with new videos and resetting you to the top of the screen.
Similarly, back button in your app should do the same. If however, you want your back button to behave differently, android does provide a way to do this. Refer to this for that.
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 am making an android app. In the action bar, I have three buttons: the up button, a button that goes to a home page (essentially a restart), and an info button that describes the app.
I have set the info button to go to an activity called 'info activity' that just has some text on it. The issue is this problem: My info activity can be triggered by multiple activities in the app, so this activity does not have one parent I can name in the Android manifest for a return. I cannot find any documentation to allow one activity to return to multiple activities, depending on which the activity the 'info activity' was accessed from and use the up button navigation to return to it. Is this impossible? Or is there another way I can do what I am attempting? It seems like one activity can only have one parent.
The "Up" navigation is designed to work in a hierarchical structure and by that, I guess that means predefined structure. It's not meant to work for dynamic, ad-hoc structures.
From Android designed guide:
The Up button is used to navigate within an app based on the hierarchical relationships between screens. For instance, if screen A displays a list of items, and selecting an item leads to screen B (which presents that item in more detail), then screen B should offer an Up button that returns to screen A.
If a screen is the topmost one in an app (that is, the app's home), it should not present an Up button.
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 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