How to switch between different activities like tabs in android? - android

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

Related

Multiple activities for displaying many images?

I developed an app for the iPhone that I would like to make for Android. However, because I was self taught with Xcode, I approached the project incorrectly. My app in it's basics loads a certain picture from a List View when the user selects the certain button associated with the picture they want. I had no programming experience when I started, so I just linked by drag and dropping roughly 300 buttons to 300 different scenes which displayed 300 different pictures in Xcode. Yes, it was very time consuming... My question is how should I approach my project to do the same function in Android Studio, but minimizing the amount of activities. I have the idea that it would be more beneficial to use code to have one List View activity that incorporates many buttons, that when pressed, will display the particular picture. Any recommendations for tutorials or videos as a place to start as I learn my way around Android Studio? Or am is it hopeless and I will have to have another roughly 300 Activities in Android Studio.
You can use fragment for acheving your purpose.
A Fragment is a piece of an activity which enable more modular
activity design. It will not be wrong if we say, a fragment is a kind
of sub-activity
Fragment you can think of it like a sub activity, which sits with in
an activity and which contributes its own UI to the activity screen.
Fragments are always part of an activity. With out an activity, a
fragment will not exist. So your fragment life cycle will always be
affected by activity life cycle. An activity can contain more than one
fragment. Similarly a fragment can be re used in multiple activities
Below links will help you to know more about fragment and how it's implemented in an android application
https://stackoverflow.com/a/13757241/4247543
https://www.raywenderlich.com/117838/introduction-to-android-fragments-tutorial
https://www.tutorialspoint.com/android/android_fragments.htm
https://developer.android.com/guide/components/fragments.html

Is it a bad practice to have only one Activity in your Android app that uses fragments?

I'm wondering if this is a 'no no' in the Android community.
My app just has a MainActivity and uses a ViewPager and TabLayout to navigate across the fragments in the app.
The only problem I see is if the user presses the back button, it will exit the app and the app will not stay active like it would by pressing the home button.
Your thoughts?
Nice question bro,
Few months back I was thinking in sameway.
You are 100% right, you can do it without any trouble, it only depend on your project and what do you want to achieve.
You could control your fragments from a single activity, beacause all fragments are independent of each other.
The limitation is :
One fragment should never talk directly to another fragment, you have to go through the parent activity
Only some imp points are:
You need to learn all details about fragment.
You have to manage the order of the fragments.
It add lbit complexity in code
One Activity and all other Fragments

Android Fragments or Layouts - advice needed

I am developing an application which should display a number of tiles on the first page. Tiles are generated dynamically from json, each should allocate itself according to size specified in json and should take as much screen as required. Each tile represents short summary of information. The requirement is that when tile is pressed user is redirected to another page which provides more detailed info (like a form) which takes the whole page. User then should be able to go back to previous page and choose another tile if needed or go back to the first one. I don't know in advance how many tiles there will be and what are their components, so everything is dynamic. There is also a possibility that small tiles(with different info) can be required to be drawn on detailed view.
At the moment I am on the stage where all small tiles are displayed on the first page and I need to find the best way to display detailed view and allow user to navigate easily and quickly. Each tile extends RelativeLayout because of absolute positioning of components inside. I am considering switching tiles from Layout to Fragments because they seem to be providing flexibility required and many articles and tutorials I search refer to them. In this case when user presses the tile fragment, all existing tiles would be replaced with required detail fragment. Pressing back button would replace detail fragment with previous smaller ones on the screen (would it be display all of them or only one?).
Another option I am considering is to leave layouts and on tile press redirect user to a separate Activity with detail view. In this case navigating back seems to be destroying activity and it will need to be redrawn again if user wants to come back to it (redraw is not desirable).
My question is what is better for performance. Each tile as well as detail view might have some images in it and full page will take time to load. But figuring out how to handle this with Fragments programmatically might take a while and the last thing I want to find is that Fragments are not suitable. Maybe you have other ideas for scenario described? Any good tutorials/articles where Fragments are created and managed programmatically completely(no XML).
I am relatively new to Android and completely lost now.
Edit:
Thanks everyone for your advice. I can't choose the best answer at this point. I have to do some more research and learning now. Will do that later.
Fragment should be the best way to go. because filling details in a fragment dynamically is easy. will help check some codes i have written that could solve this
Fragments are a new style in Android for creating GUIs, they should not be compared with simple Activity + xml layout's in performance terms. Fragments were created to make it easier to build complicated GUIs, on both phones and tablets. You can create low performance GUI using both methods.
From your description I suppose its best to create two fragments, and wire them in Master Detail pattern. Master will be your json list with short summaries, and detail will be your additional data fragment. You can still put both fragments in separate activities, and show detail fragment from master one (master actitity will get hidden) - this makes sense on small screen devices. But you can show both fragments on one screen on tablets. See 'Master Detail Flow Template', http://developer.android.com/tools/projects/templates.html.
So fragments gives you a lot of flexibility to modify your UI, without huge code rewrites.
Some new widgets like ViewPager will work only with fragments, so if you want to use it you better invest time in learning them.
From what you have described above you do not need Fragments to do this. On your main page you can use a GridView to display your tiles. You could create two other Activities. One called TileActivity which will open each time a tile is pressed. Then you could create a PopulateActivity which would populate the TileActivity with the relevant information depending on which Tile was pressed. In terms of performance instead of closing the TileActivity to go back to the main page you could use Intent Flags so that the TileActivity isn't closed it is just added to the stack and then restarted instead of recreated each time its called.

How to save a screen state in Android similar to the BackStack?

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!

How many activities should I use? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm starting Android doing an application for searching restaurants, and some guidance would be welcome!
On the first screen I'd like to have a search field with a submit button (I get the data from a web service), and below a list with the results of the search. When clicking on one of the items of the list it will show a screen with the restaurant details as well as a map showing its location. My questions are:
Can I do everything in one single activity or should I do an activity for the search, one for the result list, one for the restaurant description, and another for the map?
Would doing one single activity make the application more responsive?
How can I use a list and a map within a normal activity (without ListActivity and MapActivity)?
Can I do everything in one single activity or should I do an activity for the search, one for the result list, one for the restaurant description, and another for the map?
The answer to this really depends on the flow of your application. I think the most important thing to keep in mind here is how the user will control your app with the "back" button. When you create a new Activity, that puts it on the stack, and the user can always press "back" to pop it from the stack.
One extreme is to put all these steps into different Activities. Then the user has ultimate control using the "back" button, but they might get annoyed jumping around Activities. Putting it all into one Activity is the other extreme, and I highly advise against that; users expect new screens to be a different Activity, one that they can "back" out of, and so if you put all your eggs into one Activity you'll have to start handling "back" yourself.
I think there's a good middle ground that your app could take, though of course your UI design may differ than what I propose. I would say you could do this in two Activities; in the first, you have a search field at the top (with a submit button next to it), and below that search field is a ListView which is populated with results. In the second, you use a TabActivity, where one tab is for the description and the other is for the map. I think this is advantageous for two reasons: on the first Activity, the user sees the results of their search on the same page as the search, and can quickly change the search parameters if necessary. And on the second Activity, the back key encapsulates backing out of one restaurant.
Would doing one single activity make the application more responsive?
Not really. Activities take time to create/tear down, but not that much time. It's better to segment your application in a logical way (for the user experience).
How can I use a list and a map within a normal activity (without ListActivity and MapActivity)?
You can get away with a ListView inside of a normal Activity without ListActivity; just include a ListView in your Activity's content, then in code grab the ListView and set its adapter manually. All ListActivity does is add some handy wrapper functions for one primary ListView, but it's not necessary at all.
Maps are a different matter. You will need to use a MapActivity for displaying maps, because MapActivity has special setup and teardown code that needs to run. Sorry.
I would like to add some guidance to Daniel's excellent answer.
Users can't tell the difference between one activity with views that change and multiple activities each with their own view - until they press the back button. So it is actually quite important that the back button should have a natural and logical purpose in relation to what is on the screen when it is pressed. The user should never be surprised by what happens when they use it.
So for each activity you have, ask yourself if the behaviour of the back button is logical to the end-user at all times. If you find a scenario where it is not then you should look at refactoring your activities - which could involve combining two or more activities into one, moving some responsibilities from one activity to another (but keeping them both), or even splitting an activity in two.
Creating a user interface that behaves logically as the user navigates around it is important, and the more your application does (and the more navigation there is) the more important it becomes. Unpredictable navigation behaviour stops people from learning how to get the best out of your application at the time when they are most likely to uninstall it - in the first few hours after downloading it.
I would have an Activity with both the search and list, then another that shows the details of the restuarant.
I don't think it would work well with just a single Activity.
Activities can contain multiple views - a single activity can contain a map and a list if necessary.
If you are looking to write modular, reusable and portable code, one activity is the way to go. Activity is an Android-only concept. The main flow and the business logic of any well-designed application should be platform-independent. Involving activities in your higher-level code ties your project to Android, which goes against platform-independence, a core principle in both Java and quality programming in general.

Categories

Resources