How to navigate between views? without losing data of the initial view - android

I have a login page which has a Advanced button.
The button sets a view where a user can add some information.
When I click save, it goes back to my main view (loginpage) and all info is cleared?
How can I save the state of the first view when navigating away and then back to it?
Thanks

Take a look at the activity lifecycle, described in the activity docs. When an activity goes into the background, as when you start a new activity, it can be deleted and will be recreated when the user goes back to the activity.
Additionally, your view will be recreated when you go through a configuration change, like a change in orientation.
In your case, you probably want to save the state in the onPause() method, and reset it in the onResume() method. One easy way to share state between activities, is via SharedPreferences. See Saving Persistent State for an example.

Related

Restore fragment's previous state on rotation and position in backstack

I have an app with a single activity showing a fragment to the user. On a click on a button, the activity shows another fragment but keeping the previous one in the backstack. I have now two problems:
On rotation (particularly in the second fragment), the data entered in the EditTexts is cleared
I want the backstack to be also restored with the text previously entered in the second fragment.
The problem is that when I save data to bundle using OnSaveInstanceState(...), I successfully get it again from OnViewCreated(...) but when I use editText.setText(str), the text is not shown.
At the moment, I think this is because the fragment is then destroyed and recreated by the parent Activity.
How can I do to make it work properly?
Thanks.
Make your fragments retainable:
setRetainInstance(true);
See the following guideline on orientation change state android orientation changes `

Save & restore Fragment's UI state when pushed and popped out of the backstack

Background:
I have a main Activity, it wraps a main Fragment that can be changed, and in order to keep a backstack I use FragmentManager's backstack.
The main difference from keeping an activity stack is that when a fragment is pushed to the backstack and get replaced it will call it's onDestroyView() but not it's onDestroy(), and when it get back it's view will be re-created with onCreateView(). (however onCreate() is not called as the fragment object is not disposed)
In an activity stack it won't happen and the views remain.
This has a positive effect on low-end devices as the Android OS can free some memory and you don't have to keep the views right (in my app messages from the server might change the view in any time) so one can save precious bandwidth as well.
The Actual Problem:
Let's say I have a fragment and the user click on something and it's view is changed, e.g. a list is expanded.
If the user then go to another screen (i.e. fragment) the previous fragment will be pushed to the backstack and it's view will be destroyed.
When the user is going back, the fragment will be re-created and will not "remember" the changes the user had made, e.g. the list would not be expanded as it should
so how can I save the state and restore it without making special cases for every view?
Undesired Answers:
keep the view alive: doing something to keep the view would break the fragment efficiency
using onSaveInstanceState(): it will not get called when the fragment is pushed to the backstack as the activity is not destroyed and that's not a configuration change.
special object: prefer not to do it if there is a way the system can do it for you.

Save Fragment at Activities switching

I use PullToRefreshGridView (it's like ScrollView) with infinite scroll in a Fragment. PullToRefreshGridView contains many ImageViews, images downloaded from the Internet. User's click on ImageView starts new activity with the detailed information about image. The problem is when the user press back key in the DetailedImageInfoActivity, PullToRefreshGridView starts reload all images and loses it's scroll position. How I can avoid this?
You should save all activity data you want to reuse when going back in the onSaveInstanceState() method and restore the data when the activity comes back to the foreground in the onCreate() method.

how to maintain focus between two screen component?

In my two screen app first screen has some button, from here control jump to other activity. Now when control return to first activity from second, how can focus for last action item?
Also please suggest how can do same thing while communicating between two fragment?
Have you tried calling requestFocus() on the control in your first activity's onResume() method?
If you need to remember which control was tapped (and therefore which one should have focus), you can save a reference to in whatever code starts the second activity (such as that control's OnClickListener), and use that information in onResume().

Save screen position when changing to different activities

I have a relativelayout with 30 buttons within a scrollview. What I would like to know is if there is a way of saving the position, ie. when the user has scrolled down to the second last button and clicked on it to display an image or text, and when pressed back to select a different button to return to the last position (which was the second last button for example) without having to scroll all the way down again? I had a sliding drawer and it worked fine, but with the buttons (which are image buttons) it takes a while to load the screen with all the buttons. And I found doing it without the sliding drawer it loads faster but now have to scroll all the way down to the button every time I return to the buttons.
Android docs say here:
[...] you should use the onPause() method to write any
persistent data (such as user edits) to storage. In addition, the
method onSaveInstanceState(Bundle) is called before placing the
activity in such a background state, allowing you to save away any
dynamic instance state in your activity into the given Bundle, to be
later received in onCreate(Bundle) if the activity needs to be
re-created.
Perhaps you just recreating your activity content by accident.

Categories

Resources