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
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.
I have realised an NFC reader application
So i have 3 activities :
MainActivity, which is an activity who contains a Button. If button is clicked, the scan is activated and the user can put his NFC tag against the device to detect it.
WebActivity, who is launched if the NFC tag contains and URL (and open a WebView) or if the user want to launch WebActivity by himself
HistoryActivity, who gonna contains a list of every scans.
Now, I would like to swipe activity with a finger gesture. according to my research on Internet. I need fragments and ViewPager.
But every example that I saw is bases on ONE activity and multiple fragments.
But in my case, I have to create 3 fragments (one per activity), right ?
And I really don't know how to manage my fragment. I mean, what to put inside ?
All I want to do is create a transition/animation while changing activity... That's crazy
This is too broad of a question but hopefully my answer will steer you in right direction.
You should definitely go with single activity/multiple fragments model. Aside of recommendations by Google, you could use navigation components, deep linking much easier then without single activity.
Yes you should be using ViewPager for the purpose (and likely your implementation of FragmentPagerAdapter as well) however I do not understand what kind of swiping will you be doing
Reading your setup, I would suggest to use bottom view with 2 items (good example is here https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample). 2nd one would show history, first one would offer a button that activates your action, and then displays fragment with your WebView.
As a side effect of such implementation, you'd be able to go back from 2nd bottom view item to whatever first one holds - by pressing system back button - which I think is nice touch.
UPDATE to "swiping takes place anytime. " comment:
You could have single activity, ViewPager with 2 fragments. First fragment would display a button, 2nd fragment would display a history. You could freely swipe between them, as you want to. However to me it does not make sense to put WebView screen into this. WebView screen is result of action (NFC detection) and it should probably display as full screen, without any chance of swiping between main/history and itself. Hope it helps or I'm missing some important piece of info you did not share.
I have an android app that primary receives input using a d-pad (up, down, left, right, OK, back)
The app runs on a TV so uses fragments to separate sections.
I would like to keep directional buttons from focusing views in other fragments. And handle directional buttons differently depending on what fragment is set as the "current"
What would be the most elegant solution to this?
also, Fragments are dynamically changed at runtime.
When the d-pad is the only means of navigation for a user, such as on some Google TV's, then it's not a good idea to stop the navigation at the edge of a fragment. How I solved this, is by allowing cross-border navigation and laying out elements so that the navigation is not awkward and works by finding the path automatically (after all, there does not seem to be a way to statically provide the next element to focus on in each direction, when in another fragment).
Like you I'm looking for reference advice on how to deal with fragments and navigation on Google TV.
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
Hi i am working with android , and i have a problem with the activity stack. As i know, when someone uses the back button, reload the back activity. But in the case i have many layouts shown from one activity, how can i go back to them.
Here is the deal, i am using a listview filled with categories, and when i press an item, i reuse the activity and the layout, to show its subcategories. So what i need is to came back no to the back activity, not to the back layout, but to the back "state".
Well, the idea is simple, first i show all the categories with no parent, then when i pressed an item, i show its subcategories.
The easiest way is creating two Activities - for categories and for subcategories. If you try to implement all the logic in a single Activity you won't earn nothing and just end up totally confused. Using Activities simplifies things a lot just because it handles problems such as yours. Hope this helps.
Check out Fragments, they are the stepping stone between a view and an activity. An activity can have multiple fragments and will manage their back stack (if you tell it to).
http://developer.android.com/guide/components/fragments.html
You'll have to use the support library to used them on pre honeycomb devices.