Saving/loading an Android layout with previously programmatically added buttons - android

I have a very simple question about loading layout in main activity. I have a simple layout defined in activity_main.xml which is loaded in MainActivity's OnCreate() using
setContentView(R.layout.activity_main).
Then on a button click, I add another view item (button) which shows up correctly.
But when the app is closed and opened again, I need to retain what was added last time when the app closed. How do I do that?
I searched some questions here, but most of them talk about saving values using SharedPreferences or saving state, but it is not clear if the layout can be saved as well.
Thanks in advance.

I think the best solution is saving latest state values by using SharedPreferences, because it is a good way to save any simple data for long term.
Saving state via using saveInstanceState is a short term solution and if you close your app completely, saved instance will be gone forever.
Possible solution:
First, you cannot save a layout as you think, but you can get the layout's parameters and other features as variables, then you can save these by using SharedPreferences.
Secondly, you should check if there is any saved layout states when you start the activity. If any, you can use the pre-saved parameters for adding your layout dynamically/programmatically.

Related

How to save and recreate layout in Android?

I am currently working on Android app which is like a simpler version of Photoshop in functionality.
So, I created a CustomView called DragAndDropContainer which handles all the drag and drop operations. The user can drag other CustomViews in it like Text, etc.
The problem is that whenever user clicks on Done, I want to save the layout and restore it later on when user reopens the app.
I researched and I was not able to find the best way to save and restore a layout. The DragAndDropContainer is a CustomView where user can drag anywhere, so I need to understand how to implement a method to save its final state and restore later.
Thanks in advance!

In Android,How to save a "dynamically created layout" and load back when I reopen app?

When app is running, users adds some views in the Layout. I want to store this Layout, so that when i reopen the app it should return the same state.
How can I achieve that?
Use onSaveInstanceState() and onRestoreInstanceState() callbacks to save and restore your views.
More at Managing the Activity Lifecycle and Recreating an Activity.
[EDIT] - As per #kadatt comment.
I think what you are trying to do is possible duplicate of android-saving-state-of-dynamically-changed-layout.
I dont think there is any standard way of doing this. You will have to implement your own functionality to save state of dynamic UI in some form which can be loaded again.

Android save programmatically created button

In my program i create buttons programmatically on OnTouch event but when i reopen the app or activity get restarted these button do not appear
Can any one suggest me how can i create buttons programmatically those will stick to the layout even after the activity gets restart or app gets restart
Use SharedPreferences to store coordinates value.
As suggested in Comments saving state in shared preferences is good option .
So you can go with this links to know more about this topic, I think it will be more clear when you will look into all this .
Save instance of dynamically generated views when we switch back and forth between activities
How do you save a (user-created) dynamic Layout in Android?
android save programmatically created views
Android - How to save a programmatically created view and restore it on app restart

Saving the list data on screen orientation in Android

I have one datepicker and one search button. On search button click, I have to pass the selected date to backend and display the fetched data in listview below search button.
In portrait mode I am able to display the data listview after clicking on search button. But when I rotate the emulator all the data getting lost.
How can I preserve the searched data to display in listview in landscape mode.
Please help me. Share some example code if possible.
android:configChanges="orientation|screenSize"
add this line inside your activity in manifest file.
then data will not lose during orientation change
Look into using onSaveInstanceState and onRestoreInstanceState. Or you could save the data you need into SharedPreferences, however, personally I'd use the InstanceState methods
You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape") to in your manifest.
you could distinguish the cases of your activity being created for the first time and being restored from savedInstanceState. This is done by overriding onSaveInstanceState and checking the parameter of onCreate.
android:configChanges="screenOrientation" in the tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).

Android: Single Activity & Multiple Views

I have a very specific situation where i found that a single activity generating multiple views is the most correct approach:
The main activity receives a code from the server (XML like) saying what it should build. That XML can contain links to other views that use similar code.
To use this, i build only one activity that decodes the code sent from the server and builds the view...
When i pass from a screen from this activity to another screen of this activity, retrieving more server-code on press of a button, it's all ok. But... when pressing back, the last view has also been altered.
I understand this, the activity being used is probably the same.. how can i avoid being the same?
You can use a Singleton Class which is initiated only once and retain values.
Found the problem. Really stupid... I had a static variable that was accessed onResume so it messed all up...

Categories

Resources