Save screen position when changing to different activities - android

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.

Related

How to move back and forth among many images on a click of back and forward button in android?

I am creating a book app in which i have many text images around 60. I want to move back and forth on click of forward and backward button, just to give changing pages feel where user can read the pages.
Also i am not sure whether to use fragements or a whole activity to show the images.
If the content is static, I do believe the best approach would be to make an array with pointers to each image, then, have an activity that loads the image and make an animation to "swap" pages.
Also, how is the hierarchy you are using? Does the user use the hardware back button to go back pages, or are you using left/right buttons to swap pages.
In the first case, do note that you could create a lot of activities that serve no purpose.
So:
onCreate() Start the program, do your styles and etc. then load the image[0]
onResume() Update the image, do the transition effect
onPause() Save the index of the image array, and the direction the user is going (left/right), then decrement/increment the value, and wait for the onResume, or start this activity again with the android:launchMode="singleTask" modifier in that "view book page" Activity
onDestroy() Destroy everything you created to free the memory.

How should I save state of Fragment that contain a listview on android?

In my app there are two fragments and one activity. On all of them I used listview and fill the content from database. When rotate screen the position of content in activity don't change and start on correct way (as I googled and understand it is normal for activity because of "Bundle savedInstanceState"). My problem is with fragment part that both of the list on fragments are too long list and it could be annoying for end user when press back and came back to fragments start navigate to appropriate item. I googled and find some answer to my question but they don't work for me.
In this case, the states saving or restoring won't work anymore, because the listview was dynamically fill up by database or internet. when I facing this problem, I have a long size dataset for that listview, the rest datasets are coming because my data was show as pagination mode, user may scroll out far from first page, so when fragment destroying, all you can do just save the particular parameters to State Bundle, e.g. the page number, the selection index, the selection y-axis, finally restore the state by your own hands.

Manipulating layout on a previous activity in Android

I have an application that has a list of buttons created dynamically in the code. When one of these buttons is clicked, a new activity containing a similar list of buttons is added on top of the stack.
These lists of buttons each have a different view for portrait and landscape, so I am overriding OnConfigurationChanged to lay update the existing buttons because it is faster than letting the view recreate itself.
When the second view changes orientation, even though the previous activity is in the background, it performs the changes to the buttons in its list.
The second activity is also transparent and you can see the previous activity through it. Normally you won't see the first activity from the second activity, but the entire second activity is in a HorizontalScrollView and the user can slide the view off the screen which then finishes the activity and goes back to the first activity.
The problem is that when there are hundreds of buttons on each activity and both activities are being updated on orientation change, things slow down considerably.
In all my cases, I have noticed that things go smoothly if only one activity is being updated on orientation change. Is there any way I can postpone the previous activities changes OnConfigurationChange or speed this up in general?

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().

Categories

Resources