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.
Related
I have a simple app, in the home screen it has 4 tabs. Each tab is a fragment. In one of the fragments, I have a ListView that displays some songs. Nothing is stored locally, I populate the ListView with network calls (to the iTunes API, to my own server). If I click on a list item (a song), it starts a new activity that searches Youtube for that song. When I click back, it's as if the previous activity is started from scratch. How do I retain the scroll position of the ListView ?, It also goes back to the first fragment (the leftmost one in the viewpager). How do I make it return back to the fragment that the user clicked the song in?
You can try overriding the onBackPressed method in the called Activity and call finish() in that.
I'm new to the Android ViewPager.
I have a ViewPager with WebViews on each page - basically slides of web pages. Some of the pages have text input. I want to save the user input of the pages into a Java object, so I can send them to the server, as well as restore them later when reopening the ViewPager.
Any idea how to go about doing this? Originally I thought I should save whenver I change pages (OnPageChangeListener), but I can't figure out how to pull up the Fragment of the last page, so I can get hold of the input fields from the WebView to save. I need the save to occur so that the ViewPager always displays the correct data, e.g. when sliding between pages, switching orientations, pause/resume app, etc.
Thanks very much in advance.
well can go like this
Catch the user input using javascript bridge,have a database to store all the user inputs.
Then use the same user inputs while sending to server, or while restoring the webpages.
OK, I think I have figured it out:
Rather than saving when flicking between pages, I'm now saving the data in the onPause() of the Fragment itself, i.e. the Fragment for each page. The Fragment's onPause() is called when:
The Fragment is about to be destroyed (when exiting the ViewPager, or when the Fragment is outside of the ViewPager's cached range)
When the app itself is paused, e.g. switching to another app, or when it's killed
When a child Activity is opened
When the orientation of the device changes, causing the parent Activity to be recreated
This seems to the most reliable way of saving a Fragment's data.
Any comments or suggestions on this?
Thanks again.
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.
I have a ListView that is dynamic and will constantly change. The user selects an item and goes to a different activity.
What I want: When they hit back button and return to the ListView, I would like to call a certain action to refresh ListView (in my case an AsyncTask).
I have figured out one way to do this: By adding my refreshing code in onResume. But I find it refreshes a little too much -- I only want it to refresh when coming from the forward activities.
I have figured out one way to do this: By adding my refreshing code in onResume. But I find it refreshes a little too much -- I only want it to refresh when coming from the forward activities.
put your "refresh" code inside of onStart() instead of onResume(). onStart() gets "Called when the activity is becoming visible to the user." - Activity Lifecycle
Which means that it will happen only at the time that you activity gets put onto the screen.
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.