I have a question. This is possible to call onSavedInstanceState() and onRestoreInstanceState() manually. I need that because I want to recreate all Activity but I want to save last all views. This is schema: I have in activity three toggle buttons, I check all buttons and click on button refresh. Now I want to recreate all views and save all states my buttons and other layouts. Something like onConfigChange() but without changing orientation.
Related
I've created an app with two activities. On the second activity i can create new buttons by pressing a floating action bar button inside the activity; but the problems is when i return to the main activity and come back all the added buttons are gone.
I was wondering how can i save these added views so whenever i exit that particular activity or the app entirely, views won't perish.
I know i can save the views' values by "Preferences" commands but that only can save the views' parameters not themselves and if i go that way i have to recreate each view again each time i enter the activity.
if i could update the activity whenever views are added to it that would be excellent.
I would appreciate any help.
You can save information about views in preference like number of buttons created. for example you clicked button 4 times then save numOfButtons value as 4. In onCreate method you can check values of numOfButtons and execute that block of code 4 times to generate buttons.
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().
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 know you can use this: android:configChanges="orientation" to eliminate redraws on orientation changes. Although it does not in my app. I've read that there may be a method you need to override with it to make it work. But I think that may be overkill.
There are only two issues I do not want to happen when I rotate the screen from portrait to landscape (or the other way).
If the user touches an EditText, keyboard pops up. You shift orientations, and it auto-hides. I want to keep the soft keyboard along for the ride.
I have a ListView loading data populated from a MySQL database. It does this through an AsynTask. When I switch orientations, I do not want this task to be called.
Can I isolate these two issues, or is the first option (configChanges) the answer?
Note: A couple of these are List Activities; but the big one is a FragmentActivity with Viewpager, and a Fragment / ListFRagment (with tabs) inner class inside.
When the orientation changes the Activity is destroyed and recreated. The method you would override if using configChanges is onConfigurationChanged.
For the keyboard question, sounds like you just need to keep a track of it's state and restore it via the Bundle passed in Activity.onCreate.
For the second question, you could check if onSavedInstanceState == null to determine if you need to run the AsyncTask. You will need to save any data to the Bundle in onPause.
I am working on a tablet application with two fragments on the main activity. The left fragment is a list fragment whose contents is updated when the user selects a tab or selects an item from 2 spinners within the ActionBar.
Originally I handled orientation change by overriding onConfigurationChanged. As this is not advised by Google and it causes issues with ActionBarSherlock, I have started work on doing it the right way. I have set my fragments to retain their instance (setRetainInstance) for orientation changes.
The problem is when the orientation changes the OnCreate method for the activity adds the tabs to the actionbar and selects one causing the list to reload. This also happens with the spinners as on rotation a new item is selected. On orientation change the list fragment has no need to refresh.
I know it is possible to save the tab and spinner state but how do I stop the list from updating as this is done in the onTabSelected and onItemSelected methods?
How about using onRetainNonConfigurationInstance() to save some state which should not change on orientation change? For example, if you follow #ThomasKJDK's answer , you could set the boolean in this method. You could then retrieve this boolean in onCreate(); and decide on execution of various code segments based on this boolean.
More details about this approach here.
I don't know if this is the proper way of doing it, but you could make a local variable (boolean) which is set after the first OnCreate and reset at OnPause/OnDestroy. You can then use the created variable to exclude execution of some code segments in the onCreate method.