How can I save state between orientations? - java

In a previous question I asked how I could display two different layouts for portrait and landscape orientations:
Problem switching to landscape orientation
Now I would like to know how I can save the state of my application before it goes into another orientation? I seem to lose this data any time orientation changes.

This kind of quesiton has been answered before.
But there are also other ways not listed in the previous answer. For example using the onRestoreInstanceState() method, as explained here. This method is the advised way of storing state between configuration changes, and was poorly documented until recently.

Related

Set different constraints for each orientation in Android Studio

The question sounds way harder than it is. Simply all I want to do is to be able to set a different layout for, I already added a landscape orientation, and recreated the whole design the way I desire in landscape. However, the app restarts each time I rotate the phone, if I add android:configChanges="orientation" in manifest, it does save the state but not the way I set in landscape. The app is 4 layouts in each orientation. So if I could just make different constraints for landscape the doesn't interrupt the original portrait it could solve my problem.
sorry for the poor explanation. It's my first week with Android.
You are correct in designing different layouts for different orientations.
For the state, you should override the onSaveInstanceState() method in Java.
Using that, you can save the state of the application and then inflate the layout the way you like in Java.

Keep ViewModel alive even though there is no screen rotation

It's well known that on screen rotation activity will be recreated and we need to survive configuration changes if we follow the MVP or MVVM patterns to keep alive Presenter or ViewModel(Example: to avoid double calls to WebAPIs). The question is, do I need to keep alive my ViewModel or Presenter if by business requirement we don't have screen rotations(only portrait mode)? Thanks a lot in advance.
Short answer: Yes, you do.
Long answer:
Orientation change is one of possible configuration change events, there are others (like Locale change, hardware Keyboard open/hide, screen size change (due to enable/disable Split Mode) etc.).
Moreover, configuration change is one of possible causes of activity's recreation - it can be re-created w/o configuration change by the system when the activity is in background and system goes low on memory. You can simulate this with "Don't keep activities" developer option enabled.

How to make a button that draws over any screen?

Apps like Floating Toucher or Link Bubble proves how useful a floating button can be. And no, I'm not talking about FABs as per Material Design guidelines here, I meant a literally 'floating' button that draw over any screen in the system that can be dragged around and clicked.
Take a look at these examples:
(source: boatmob.com)
Now, I tried to Google a way to do this but I'm afraid I'm still at lost here. How can these apps draw over screens outside their own?
I've come across this SO question which more or less asking about a similar question. But to be honest, I wasn't able to gain much information from it. Said question mainly addressed how to intercept a touch for a floating view; Not how to specifically make one.
I really hope that somebody out there could enlighten me on this. Thanks in advance for your time!
If you are talking about an activity's view (not Widgets), 'floating' on screen, like Facebook ChatHeads. Then you should create a background service and attach a view with it. Though it is not recommend by Android.
See below links
What APIs in Android is Facebook using to create Chat Heads?
http://www.piwai.info/chatheads-basics/

What is "focused" regarding android apps?

I keep seeing "focused" and hearing about "focusing" within the development of Android apps. My question is: What is focusing, and how is it applied within Android apps? Is it important? What can you do with it?
I'm sorry if this has been asked, I looked but didn't see anything that clearly explained it. I've looked the the Android development guide, but I couldn't find a decent explanation of what it is and how it works.
Focus is simply giving a specific view "focus", or attention.
One example of using focus is if you have an EditTextView and you want a user to be able to type into it as soon as it is displayed, you would give focus to that EditTextView and the keyboard would automatically be displayed in accordance to typing in that view.
If you've ever used a blackberry and browsed the web via that, you'll remember that whenever you scroll the wheel you sort of select random elements on the page until you find what you want, then click enter. You know what element you're selecting when the elements change colors, or become focused. That's essentially what's happening with 'focused' on ANdroid, except it's mostly used for when you're scrolling via some kind of buttons, or when you select a textbox or something and it's waiting for input.
It is just referring to the view that is active. For example, if you have a TextView and it is the active view then it would be in focus
Simply put, an object/item that has the user's attention for interaction is focused. It is a state an item can be in.
Android Dev UI Events

Android - switching between listviews without changing the layout?

I'm not looking for exact code here, just a direction on what to look for and what I should be reading about so I can figure this out.
I have a layout that I would like to remain static, with only the listview changing depending on what's selected from the list. I've reloaded data in the list, but I would like the fancy transition animations between choices, and would like the app to go to the previous menu when pressing back.
Someone suggested using a viewswitcher, which seems like it'd be great, but I am still unsure about how to fill a listview in a layout with a regular row layout, then on selection do an animated transition to a custom row. Also, it seems the viewswitcher is limited to two views, so it may be a limitation when I want to go a few menus deeper.
Preferably, I'd like to put each menu in it's own class so that I can handle filling it in that class, if possible...
Hope this isn't too vague, but if it is I'll be more than happy to explain myself further.
Don't know if it's what you meant but have you tried showing and hiding the views?
findViewById(R.id.listViewID).setVisibility(LinearLayout.GONE); //hide the one you want
findViewById(R.id.listViewID2).setVisibility(LinearLayout.VISIBLE); //show the one you want
Hope it's what you meant :)

Categories

Resources