How to create and manage a multi tab Android app? - android

I'm trying to add multi tab functionality (browser like) to an already existent app, a mobile client for Netbox, and the goal is to implement a multi tab environment to manage more items at the same time.
This app is written in Kotlin, using android navigation (single activity) and MVVM.
Searching on the internet I didn't find a reliable solution, and I came up with an idea based on managing multiple instances of the main activity, because creating multiple instances it's quite simple.
If this solution can be considered good, I need to find a way to manage various instances to be able to switch between them, maybe with a navigation drawer.
If this solution it's unsuitable, every other idea is welcome.

See viewpager or viewpager2 ( Recommended ). https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2?hl=en

Related

Android Navigation Architecture Component - Is Navigation Architecture Component meant to use Single Activity Only?

I currently learning on the new Android Navigation Architecture Component (https://developer.android.com/topic/libraries/architecture/navigation/).
I kind of confuse with its motive and concept, here are my uncertainties:
Is Android Navigation Architecture Component designed to eliminate the need of using multiple Activity in a single apps? Which mean, the whole apps just need a Single Activity and all other page will be Fragment?
Does using Multiple Activities in the apps, but in the same time using the Android Navigation Architecture Component to navigate the Fragment actually violate the purpose of Android Navigation Architecture Component?
Example Scenario for Question 2:
In theory, the Navigation library supports any architecture you might want to use. Out of the box it can handle Activities and Fragments as navigation destinations, but you can plug in your own solution by implementing your own Navigator (as an example, see this article).
However, quoted / paraphrased from the Google I/O talk on Navigation:
What is my Activity actually meant to do?
Right now, some apps are very Activity-heavy, some are Fragment-heavy, or completely in a different system. We're moving towards a model where the Activity is more just an entry point into your app, rather than it being the owner of the content of your app. It's actually just going to store global state, for example global navigation like a navigation drawer or the bottom bar.
So Google does recommend having just a couple Activities for your app, because you only really need them to serve as entry points. For example, you can have one that opens from the launcher, and another that's opened by deep links. After that, when your app is started, you can do everything else inside it with Fragments.
To summarize and directly answer your two questions:
The Navigation Architecture Component isn't "designed to eliminate the need to use multiple Activities" per se, but it's something Google recommends doing when you're using it.
You can absolutely still use multiple Activities and multiple Fragments mixed together. You can even use a single Activity with purely View based navigation if you like. It's all up to you. If you find the Navigation library useful in combination with how you architect your app, use it.
The tooling of the library might not be that great for custom destinations (for example, the visual editor will probably only support Activities and Fragments for the time being), but you can use it however you'd like from code.

Android : TabActivity or Fragment? When to use Tabs, when Fragments?

I want to create an application which one of its pages must like below :
Each tab contains some Views ( like a form )
After some search, I know there is two ways for implementing above layout. TabActivity and Fragment. But I have not use neither Tab nor Fragment in my projects(cause i have short experience on Android)
Now which of these ways I should use? Clearly, I want to know which is more suitable to use? (According to the better one, if there is a brief and clear tutorial, please introduce it to me)
Note : In this case, the application must run on Android 2.3.3+
TabActivity is deprecated. You shouldn't use it, especially if it's an entirely new project.
Fragments are available for older SDKs via the Support Library, so you can use them in 2.3.3+ with no problems.

Proper usage for Activities in Android

From what I understand, and activity is equivalent to a "page" in a web app.
For example, the list view would be one activity, edit view another activity, and add view a third activity.
While this works as expected, android activities seem to operate as individual apps--the action bar is different for each activity and so are the menus.
Is my usage of activities above correct or should I be using some other mechanism such as swapping out layouts and views?
Fragments are a core part of activities - not that much different. The use of fragments comes since Honeycomb 3.0 and the idea is an option to split the screen in several fragments at once. For example if you look at the gmail app for a tablet - you have one fragment on the left dealing with navigation and then the next fragment on the right is the list of emails.
On a mobile device, viewing area is limited, so you it could be said that fragments sort of behave like an activity - you interact with one fragment, which triggers another and so on and so fort. But in the end you always reference a super activity of each of these fragments - for example when you want to access the context.
So if you just want to wrap web pages in WebViews, stick with activities. If your scenario might involve developing for both tablets and phones, then go for the fragments.
Alternatively, you can read about the design philosophies of both here:
http://developer.android.com/guide/components/fragments.html
Good luck!
Fragments are to be used when you have common feature across multiple activities. From my perspective you should use individual activities as there will be back-end code to support (fetch and validate data, i.e. business logic). This way you have more modular code. Fragments is a new feature from v3.0.
From what I know, Fragments would be a good option to be able use different configurations/real estates on different devices. For example, if you are on a device with big real estate like Tablets or TVs you can display more content in a single activity, on the other hand for a devices with smaller real estate you can show content based on a progressive manner.
See this: http://developer.android.com/training/multiscreen/adaptui.html
Note that Fragments are supported only on devices running Android 3.0, so you might have to use Support Fragments (See: https://stackoverflow.com/a/6528757/713778)
But Again it depends on your specific needs. I am not sure what your exact use case is, but I would suggest you to watch some Android design in Action for some examples to improve your design and make your app "User Centric" (See: https://www.youtube.com/playlist?list=PLWz5rJ2EKKc8j2B95zGMb8muZvrIy-wcF)
I recently came across this https://www.youtube.com/playlist?list=PLWz5rJ2EKKc-riD21lnOjVYBqSkNII3_k seems provide an in-depth analysis on User Experience.
I would recommend using fragment for web like views. Fragments are pretty snappy compared to activities and reusable.
But before you start, make sure to fragments are good fit for your requirements since they are supported from android 3.0.
You can declare fragments from xml itself or you can create frame layout and add that view in the code itself.
http://www.c-sharpcorner.com/UploadFile/2fd686/fragments/
The above link has a good example tabhost.

Android - Reading data from tabs / fragments

I'm working on an Android app that does nothing more then storing data in a SQLite database and displaying it.
Since I have a .NET background I started off developing the app with MonoDroid (C#). After developing the app with MonoDroid I thought it would be fun to redevelop it with Java and learn along the way.
In the original app I used a TabActivity and created 4 tabs to stucturize the data I want to save in a single table. Underneath the tabs I placed a button. A click on the button collects all data on the 4 tabs to store it, and it worked perfect.
Now developing with Java I noticed that the TabActivity is deprecated and I found out that I have to use fragments. I was able to create the tabs following this article. However, I am no longer able to read the data on the tabs. I searched online and tried the most ridiculous "solutions" for a couple days in a row now, and I start to doubt if the functionality I look for is possible.
Please advise me what to do! Is it possible to recreate this functionality using fragments? If so, can anyone give me some pointers?
If not, what would be the best way to approach this?
Thanks in advance!

Correct Way To Implement A Tabbed Bar

I have been browsing the web for a while now and looking at different examples of how to implement a tab bar.
The problem I am having is deciding the correct approach to creating this feature. The option that seems most used is to have one main activity called MainActivity or something and using fragments as the tabs. This is all well and good and I have created an application like this fine.
The problem I am having is I am in two minds as to whether this is the right way to go about it as coming from an iOS development background it just doesn't seem right due to these tabs having different functions.
Any pointers would be great.
EDIT I have also looked into ActionBarSherlock but couldn't get to use it in a project as it kept coming up with errors and I couldn't find a thread that sorted the problems I was having out.
Disco
You should be using fragments, yes. There is an example of how to do it here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
This "only" works with Tabs that are part of your Actionbar, though. Shouldn't be too much of a problem, because they usually should be part of your Actionbar. You can also manually move the tabs if you're not content with their location, or roll your own buttons that look like tabs.
Keep in mind that tabs in Android aren't quite the same beast as the tab bar is in iOS. They look similar and behave similar, but there are subtle conceptual differences in how they are supposed to be used. Don't use them for your basic app navigation, but rather for switching between similar "things".
If you want to employ this on device pre-3.0 though, you will have to use ActionBarSherlock. It does work fine and we're using it in several projects, so if you're having problems integrating it I suggest creating a new question for that, I'll be happy to help.

Categories

Resources