So I have a TabActivity that branches into three sub-activities (tabs). One of these activities is a ListView, which I want to branch into further ListView activities. However, I want each of these branched ListViews to also have the same tabs at the top. To do this, do I need to create a separate TabActivity and a separate Activity for each of these branched ListViews? Or is there an easier way?
Cannot you trick a user into having TabActivities, but instead simply have one ListView instance with 3 buttons on the top of an activity (Tabs) and every time user clicks one of the "fake" tabs, just refresh the existing ListView with views that are appropriate for one of those "fake" tabs. In my opinion this solution would be more efficient resource-wise and render time -wise (which anyway are dual concepts)
This is what I have in mind
You may use Fragments as proposed by MaciejGórski for TabActivityOne, TabActivityTwo and TabActivityThree, while the ListView inflation technique could still be used
Switch from the old deprecated APIs using like TabActivity or ActivityGroup into Fragments.
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support library which provides a version of the Fragment API that is compatible down to DONUT.
From TabActivity documentation.
Related
I am new in Android so correct me if i am wrong. When another activity is opened, the first one is destroyed. So i have to pass all variables to the new activty if i don't want to lose the data. Or, can I run another not UI thread that manages my data?
Basically I need to change layout in my app on button click. Is there any way to do it within the same activity or i have to start another activity with the new layout?
You can use one activity in your app and do what you want in fragments. Navigation is good tool to use for relating fragments to each other.
well initially you can use a single activity and in that activity you can call multiple fragments as per your need,
and for the data you can use MVVM architecture with room architecture, it saves your lots of time and code.
and go with navigation graph if you want a quick and easy implementation.
you can start all this with this demo.
Some background:
Coming from an iOS background, using UITabbarController is very common and straight forward. Each Tab in the tab controller will change the current view to another UIViewController, and each of these UIViewControllers can have its own NavigationController (which kind of acts as a back stack). So whenever I switch tab, I would resume to the state where I left off.
Now I want to implement the same thing in Android, but it seems like the use of ViewController is different in Android. After digging around, I read that instead of using Activity like UIViewController, I should use Activity to act more like NavigationController, and use Fragments (which is deprecated)
to act as UIViewController instead.
However my question is:
Should I be implementing multiple Activities for Bottom Navigation? When I click on each item in the Bottom Navigation should I use an Intent to change Activity? Because from my understanding, using Intent to change Activity will add the new Activity to an Activity back stack, which would prevent me from switching back to whichever Activity I want. If someone could, Please tell me what is the "right" way (if there is one) to structure Bottom Navigation. Thank you all in advance.
You can use fragments as UIs, And Use a BottomNavigationView in your activity or you can use some libraries.
Here is a library for better customization: https://github.com/ittianyu/BottomNavigationViewEx
Native Method:
https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
In Android you should use Viewpager, tablayout and Fragments. Just search for its tutorials. there are lots of them on internet
I vaguely understand what fragments are used for. However, I don't understand why ADT seems to suggest that we should ALWAYS use them. I am aware that I can simply delete the fragment layout, as well as the code in Activity for the fragment. But is it ACTUALLY recommended to use fragments all the time? What are the benefits of not deleting it?
Is there even a point of an Activity having a single fragment? What is the difference with it having no fragments at all?
Additionally, how do you know whether to create multiple fragments in one Activity, multiple Activities with a single or zero fragments or a combination of Activities and fragments?
For example, if you have a Terms and Conditions button in your Activity, and you want it to open a screen containing a document listing the terms and conditions, should I be starting a new Activity for that? Or should I just move a fragment containing this content to the front?
Using Fragments is how modern apps are developed today.
Some of the benefits are a better performance when switching content (lets say 2 fragments)
because you are not leaving the current Activity.
Also, apps with swipe tabs (for example) are built with an Activity as a container and Fragments as the content itself.
Other benefit is that you can do much more with one activity, distributing work between the fragments. Each Fragment have its own Layout and therefore its own components, so maintaining the code and keeping organized is easier.
Eclipse does this way as suggesting a start point. You can totally ignore this and start from scratch. And that's all.
I suggest you to keep the Fragment to start getting used to it. Even if there is just one.
And, if later you need to work more with that Activity, it will be easier to add new features by creating a new Fragment, without modifying your previous code.
Good luck.
I've a tabBar with 4 tabs, the Main tab is the first one, and when I click the second one I want do display another activity above the main activity in the half of the screen, reducing the opacity of the main. I'm doing this with a Dialog but it's not the result I want. What are the best way to do this?
example image
Look at Fragments, this seems like the perfect time to use them.
There are only 3 options to achieve what you want.
Second activity can be dialog (You already said you do it but you don't like).
Using fragments. (recommended)
Using layouts by playing with visibility, (not recommended in your case)
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities
I've created ListActivity and other Activities, which I would like to be displayed when I click item from ListActivity. My problem is that I don't know how to do it in the same window, so that the ListActivity will stay always visible, while other part of the window will change based on the pressed element.
You would have to have only one activity, such as by converting "other Activities" into fragments that are displayed by the ListActivity.
Instead of using Activities, I would implement your functionality in Fragments. They allow for increased modularity of an application, and will allow for the functionality that you desire, at runtime they can be "swapped out" with inside your ListActivity.
Reference to a thorough overview of Fragments on the Android Developer website: http://developer.android.com/guide/components/fragments.html