I am working on a corporate apps for internal use,
one of the sections that is presented to the user is a TabbedView (in fact I made it from the Android Studio "clean sheet", so its Fragment-based and extends AppCompatActivity), each one of the four tabs it is composed of contains a few text fields.
The user is free to navigate back and forth across the tabs and fill in the text fields, then hit "go" at the bottom of the last tab and submit the data from all the text fields.
I am finding an incredibly hard time in doing this, what I would like to do is simply to use a listener telling me when the user is switching from a tab to the next one and save the data in the shared preferences. (being also able to restore it when the user is freely switch between tabs in the editing phase just doing the same thing with the listener for a tab selection)
I was quite surprised to see that the easy old tab system with the super handy listeners for "on tab selected" and "on tab deselected" event is now a thing of the past, so after two days spent trying to figure out how to do such a simple thing as saving a few text fields right before the tab gets killed I am now surrendering and asking for help.
How can I automatically save data from the text fields in a tab before loading one of the others?
thanks! :-)
Read about fragment life cycle on the Android Fragment documentation page.
Save your data in onPause.
Related
I am building a Xamarin Android app and need to make a design choice. I am still rather new in Android development, and don't know any other developers in my area personally that I can ask for help on this matter.
The app has a "BottomBar" with several buttons / tabs. If a button is clicked, a new screen should appear.
All 4 screens contain a lot of data, mostly lists (or RecycleViews?) of images with text and when clicking on a listItem or similar, an associated detail screen should appear.
At this point I wrote code that loads new activities when I click on one of the bottombar buttons. E.G., btnA loads activityA, btnB loads activityB and so on. The BottomBar is regenerated in each new activity and also keeps track of its instance State, so if I am in activityA and click on btnB, ActivityB loads and in activityB btnB is highlighted as the active tab, without me having to write a single line of code. Basically, this is working great out of the box.
I am wondering however if I should add a big fragment container in a host activity, and load different fragments in that container on BottomBar button clicks instead. Maybe this approach has a performance benefit or some other benefits that I am not aware of as yet. As for now, we don't have any intentions to create a tablet version, we only want to create a mobile version of the app. I understand that I can reuse fragments with a bigger screen estate (tablets), but this should not be necessary.
Again, everything is working ok as it is, but I am only at the beginning of building the app, and would like to make good design choices early.
The BottomBar component that I am using (Xamarin c#):
https://github.com/pocheshire/BottomNavigationBar
The above version is a port of this (Java):
https://github.com/roughike/BottomBar
Any advice is appreciated...
Sorry for long waiting.
If you want to follow the path that Google recommends in their guides that you need swap the fragments in the container by pressing the tab.
Link to Google guides
I am working on a project where I have multiple tabs, and I want the content of each tab to be set dynamically based on what is selected in the previous tab. (For my tabs, I am using the SlidingTabLayout with a ViewPager). I currently have my first tab set up correctly and have it tied to an EventBus that creates a LinearLayout based off of my template from R.layouts.tab_2 and adds the content dynamically. What I can't quite figure out is how to save my LinearLayout back into the tab_2.xml file. I have seen suggestions of saving it with SharedPreferences, but that doesn't allow me to save it as the right file type.
If there is a much easier way to do this, such as updating the tab that is already created, I am open to that instead of saving the layout back into the XML file. I am fairly new at programming in Android Studio and still trying to get the basics down.
Your first tab, as I'm interpreting it, is essentially a settings page. The user does a few things, and as a result of those things - the layout on your second tab looks different.
You need to save some variables based on user interactions. For example, if you have a checkbox the user is going to press - you would set a boolean to true when they've checked it, and false when they've unchecked it. When the user navigates to your second tab, you will check the value of that boolean - and alter that layout accordingly.
Hopefully this concept will, even in its simplicity - help you understand how apps are built to be functional.
I need to develop an application which contain these tabs shown in the image. Each tab contain a form which will be filled by the user.User can switch to any tab.
User click Activity1, Activity 1 gets displayed and user enters some data; then user press Activity 2, activity 2 gets displayed, user press Activity1 again, Activity 1 gets displayed with the data entered by the user(not the blank activity).
At the end when user click "Save" I need to get all the data from these activities and save it somewhere.
I have worked a lot in java but new to android, I am stuck in developing the UI for this scenario. However I have done this many times in iOS.
Anybody please share your experience of developing such UI.
Thanks,
Fragments will be more suitable for this scenario, the benefit are
They are light weight and faster
Managed automatically by the FragmentManager.
Data Sharing between Fragments is smoother and simpler than it is for Activities
They don't complicate the Architecture of the Application
You can have as many Fragments as you want in you Activity. Following two links can be useful for you.
A similar Thread
A Good Tutorial
Another good tutorial
If even after that you decide to use Activities, You need to think
i m just wondering about some android ui aspects where i need some advices! Might be, that my idea so far is not the best...
Basically I m working on an app, which plays streams in a player (main screen). The user can select streams in a second screen (tabbar screen), where he can switch between three different lists, each one is in one of the tabs and each tabbarclick starts a new activity (i m not using an ActionBar or sth, I just created an own UI element which consist of three icons, current one selected and the other two starting a new Activity):
ListViewActivity1: dynamically created ViewFlipper with nested ListViews (f.e. country->state->city..) from a database
ListViewActivity2: simple ListView with favorits from ListViewActivity1
ListViewActivity3: simple ListView with UserGenerated content
So far it s working great but I m starting to struggle...
Everytime the user enters the tabbar screen again, I want him to be exactly at the last ListView where he was. So basically I m looking for a way to store the different screens if the user leaves them. I came across onSaveInstanceState(Bundle savedInstanceState), but this doesn't really fit my needs. The ListViewActivity 1 is a really complex list with up to six levels sometimes, which I really don't want to transport in a savedInstanceState! Is there another way?
Actually if I go back in the BackStack, this really saves the different states like I want it to. So it is possible, I just don't find anything like this..
So, question 1: Is there a way to save a view like the BackStack does?
question 2: Is this whole idea of ui-implementation a good solution to set up an app?
thanks for any input!
have been sitting with this problem for a few days and really can't seem to find the answer myself.
I'm building an application for Android devices. The application will use a socket connection to a PC running a piece of software. The user have some buttons and stuff to change the state of the PC program - this part works well. Basicly by sending text to the PC using the socket.
I also want the Android application to handle incomming events from the PC. These events should alter the UI on the device in order to reflect the state of the PC program.
To understand my problem I will try to explain the structure of the app.
Basicly the app have four activities:
A splash activity, which shows a splash screen and after a set time launches the next Activity
A connector activity, which have an input where the user will type in the IP of the PC. When the device connects to the PC, the PC will send an xml file to the device basicly containing the state of the program. This xml file is parsed and when parsed without errors this activity launches the next Activity.
The next activity is a TabActivity, creating 1 to 5 tabs, depending on the xml that was parsed. Each tab needs an activity which will handle the content for each tab. Here I have chosen to use one activity for all tabs.
The last activity is the content activity. This is a ListActivity. When parsing the xml, items will be created and divided into 5 categories - one tab for each of these. When a tab is selected for the first time, the content will be generated, and a ListView object created for that tab. For each item in a category I inflate a layout (xml) and sets the correct graphics based on the state of the object. This works just fine. Switching between the tabs is fine aswell.
So - when switching tabs I check if the ListView for the tab is null, and if that is the case I will create the content, and if not then I will call the "setListAdapter(...)" with the correct "list.getAdapter()", where list will be the list related to the tab.
When receiving an event from the PC i have a Handler taking care of the event. This handler will parse the message, and find out what part of the UI to change, and what Object is related to this part of the UI. Then the state of the Object is changed to matter what - and if the obecjt's tab is currently open the View is changed aswell - if another View is opened a boolean will tell that an object in the tab has changed state.
I have tried to get the View from the ListView variable and change the child elements, but this do not work when the list is not the current list.
Then I tried to create a whole new ListView for the tab when the tab is getting pressed, if this boolean shows that something have been changed (just as it's created in the first place the first time) - this only works for one tab.
I've tried to test it with two of the tabs (2 and 3). The app starts on tab 1. If I then open tab 2 and then 3, sends commands to the PC from both tabs and all is fine. When the PC sends events to the app then only tab 3 will actually update. Working both when the tab is active and inactive (updates when getting active). While tab 2 does not update content on any changes.
If I only open tab 2 and not tab 3, then tab 2 will update in both cases just fine.
Anyone have any ideas to what can be done here?
I have tried all kinds of stuff with invalidate(), notifyDataSetChanged(), invalidateViews() etc, and nothing does the trick.
I really don't understand why, when creating a whole new ListView (from changed objects) that the old ListView will still be shown.
Did I shoot myself in the foot when choosing to use one activity for all tabs, or is this something else?
Hope someone can help me here.
And if some code is needed then please tell which part, as the app consist of ~15 classes and thousands of code lines, so I won't post it all :)
Cheers
I ended up with one activity for each tab content instead of the first approach where I had one Activity for them all and just switching between adapters for the listview when a tab was pressed.
Splitting it into an activity for each tab made it possible for all content to be changed no matter if the activity was showing or not.