Android TabActivity and dynamic Tabs screen orientation issue - android

I am adding Views (not Activities) to a TabActivity on demand (meaning that the activity can have zero or more tabs after onCreate is done). Over the time more tabs get added, now I need to save them in a sane way so I can restore them later on. I tried to use saveHierarchyState etc on the individual views, but they are restored without data from the editTexts etc.
So given that I have a HashMap in the activity which maps from tabkeys to views, how would you save this data and restore it?

Okay, I got it :)
First I thought I needed to set the IDs of the Views as described here -- using this approach Android is able to restore the view states. But since my dynamic tabs all use views from the same layout their children all have the same IDs, so android will overwrite the data (The state is saved in a sparse array according to the view ID, so the IDs should be unique) and all views look the same. I fixed that by manually saving the state of my views and reapplying it via:
SparseArray<Parcelable> container = new SparseArray<Parcelable>();
saveHierarchyState(container);
b.putSparseParcelableArray("state", container);
restoreHierarchyState(b.getSparseParcelableArray("state"));
While this approach works I still have to find out if I can let Android do all the work by not setting IDs for the Views in the layout xml file (if that's allowed/possible).

Related

How does one save the current view in android?

The design of the application that I'm working on is fairly simple so I've decided to use Activity.setContentView() to switch between the few different layouts it has. This all happens within a single Activity, of course.
To explain it better - let's say that I have a main layout with a simple navigation and a settings button.
The problem is that whenever I show the settings view upon click and later try to set the main view back, it is unusable. It just loads the main layout, but no listeners are set, as if I show a picture.
I've figured out that I should use the setContentView(View view) constructor instead of the one that passes the layout id from the resources. Though, I have no idea how to save the current view..
How to save the current view with all its listeners and stuff to then pass it to the constructor and save my data?

Fragment view state not saved on back press when using Android Jetpack Navigation

In my app I'm using the Jetpack navigation component and I have an issue with fragment state not being saved when navigating back to a fragment.
When I navigate from MainFragment to SettingsFragment it's via the overflow menu:
NavigationUI.onNavDestinationSelected(item, Navigation.findNavController(view));
When navigating back to MainFragment, the visibility on some views and text in some TextViews is not saved, and my state machine variable has lost its state as well.
I've read solutions where the root view is saved in a global variable in the fragment, and while this solves the visibility issue on views, the TextViews are still empty and the state machine variable is reset.
Is there a proper way to make sure fragment state is saved in this case?
If you're using view model then it can save state for you. However, that only works for simple views. For complex views including some custom views that you created, make sure that you have assigned a unique id to those as Android uses those ids to recover their state. You can use generateViewId() method on View to do so. Worst case, you might need to implement onSavedInstanceState and onRestoreInstanceState on your views.
Also, make sure you have not set to setRetainInstance to false in the xml or code.
While doing that please make sure you use parcelize annotation for your parcelable data models as this can save you a lot of time.
I hope your problem is solved by assigning unique IDs and you don't have to deal with saving state. Good luck!

ViewPager Fragment Views retaining values from previous state

I have a 3-tab ViewPager with custom Fragments (EntryFragment and CalendarFragment; the third is not relevant to the question). Now when I click a date on CalendarFragment the EntryFragment should load up with data of the new date (I have coded it this way).
Now, what happens is something strange: The TextFields in EntryFragment get changed to the new data from the new date. But the Seekbars, Spinners, Switches, etc retain data from their previous date.
I am using the following methods to set the values of the various Views:
seekbar.setProgress(int);
spinner.setSelection(int, false);
switch.setChecked(boolean);
Also, I have attached onItemSelectedListerners for these components. These are getting called automatically after onCreateView() of EntryFragment.
Could anyone guide me why this is happening? Or how to prevent it?
I'm guessing it might be restoring previous state, ViewPagers have inbuilt mechanism to restore views that had been hidden. Your data is set to correct values and restored to previous state after that. To prevent that, try to add saveEnabled=false in xml layout of views you are having problem with.

Android - Fragments or custom views? Which should I be using?

I am trying to create a layout in an activity that will look different in landscape and portrait, however it will contain the same sections, just in different places on the screen.
Most examples I find on fragments is always the list and detail, which is not what I'm looking at.
In my example I have, amongst others,
a scrolling image section (carousel)
a page title with some brief details
a carousel of thumbnails
two buttons
full details of the page
Now in the landscape layout, the image section will always be on the left side with down the right side, the page title, carousel of thumbs, two buttons and full details.
In the Portrait the title will appear at top, with image carousel beneath, then thumbs, then buttons etc.
The way it works in my head, is that each section should be a fragment, and then depending on the layout file in the respective res/layout folder corresponding to land/port, the fragments are arranged accordingly. But I'm also thinking is each section a whole fragment? Or could it be a separate view that get's loaded in, but just in a different order depending on the screen orientation.
I hope that made sense?
Does anyone have any good tutorials that explain exactly when you should and shouldn't use fragments. All the usual suspects just list the list/detail example, which is not applicable in my case.
tl;dr Can I use fragments as modules/blocks in various layouts or should I just create other layouts and display them inside the main layouts.
Fragments can be used to have split screen and to store previous state unlike views. It is difficult to manage back button with views
If you just want to change how the screen is laid out, the answer is neither. You can define orientation-specific resources (either a separate layout file for each orientation or different dimensions/constraints for each orientation) and the system will just provide you with the correct resource set for the current orientation.
That said, it sounds like you may be describing a modified version of master/detail flow. If this is the case, fragments are a good way to go since the landscape view is actually several separate screens in the portrait version of the app, so each section needs it's own state and lifecycle, which fragments provide.
Now, I know you said you wanted an example beyond a list and detail view, so here's some more details on when to use fragments:
Fragment, like activities, have state and lifecycle. Custom views do not have lifecycle and are completely dependent on the activity or fragment containing them.
You might use a custom view when you have a widget on screen that is used in multiple places and is just like any other view – bound to the activity when the layout is inflated and controlled from there. It's a way to either reduce duplicating combinations of views in your layout or to draw a custom view that doesn't exist yet.
Fragments are good when you need some state or lifecycle for a section of the app that might get used in multiple places or shouldn't be logically connected to the activity it is contained in. If you use the new Navigation Component, you actually just define one activity and then each screen in the navigation tree is a fragment that gets swapped out as the user navigates around the app. Here each child component on the screen (each "screen" that the user navigates to) has it's own lifecycle, business logic, etc, so mixing the code for all of that in the activity wouldn't make sense.
So the question comes down to what you are trying to build, and this may be a case where the best way to learn the difference is to try each option out as bit. The differences become more clear with practice using them. As a general rule of thumb, personally, I only really use custom views when I am trying to make a new view that doesn't exist elsewhere. If what I'm trying to do is simply a matter of laying out existing views in a new way, the answer is probably some trick in the layout file or layout code inside the activity. If I'm trying to make a stand-alone piece of the app that does stuff, especially if it also appears in multiple places in the app, I'll probably be building a fragment.

How to restore dynamic views to the android app?

I am creating dynamic views, Adding them to the layout those views are adding fine.But once i close the app and open it Dynamic views are not visible,I want to display those views after opening the app second time. Will you give me any idea, Thanks in advance.
You'll have to manually add the view's state to the saved instance state bundle, and recreate it from there.
This is accomplished by overriding onSaveInstanceState() on your Activity and/or View. The state will be later available in the creation Bundle (onCreate(bundle)). You can use that data to re-instantiate your views.
The correct OOP way of things would be to create a class for your dynamically created view that implements a custom onSaveInstanceState() method, and then packing it and unpacking it into the Activity's bundle.
This blog post contains details and examples on how to tackle the issue. Some more related info in this other SO question

Categories

Resources