Activity or Fragment? - android

I'm new in Android programming, researching and reading Android's manuals I've seen that you can use activities and fragments. I've understood that a fragment is a ¨small activity¨.
For example, I can create an app with buttons that create new activities or I can create an app with only one activity that open a ¨main¨ fragment with buttons that replace that with another.
So my questions are: when is more convenient to use one or another? Can I get the same functionality?
Another final question: I want my app to have a drawer similar to the Facebook app, is it a Fragment or a regular drawer with an effect?
Thanks a lot

You actually use both. A fragment cannot live without a hosting activity.
As as simple rule of thumb, use fragments everywhere that has a reusable or pluggable interface. Use activity on simple places that does not call for a fragment, such as splash screens.
For the facebook drawer thingy, this is it -> https://developer.android.com/design/patterns/navigation-drawer.html

Fragments sit inside of activities so really you have to use just activities or both fragments and activities. I would use fragments.
You really need to read this until you understand it a bit more
https://developer.android.com/guide/components/fragments.html

Related

Common Navigation drawer for multiple activities without using fragments. Is it possible in android

I am planning to start an app from the scratch and I need to know wheather a COMMON Navigation drawer is applied through out multiple activities without using a fragment inside the activity.
I tried many examples but none worked for me. Any help should be greatly appreciated.
Yes it is possible create one base activity and implement the drawer in base activity like here. and extends that base activity
As far as I know it is not possible. I've personnaly used a single activity hosting a navigation drawer instantiating differents fragments to swipe between them.
Here if you wanna do it: http://developer.android.com/training/implementing-navigation/nav-drawer.html
The best you could have to get kind of the same result with activities is playing with transitions params in each of their class. It is kinda heavy. Use fragments for this purpose.
The following topic could bring you some work elements if you still wanna do it: Android how to add swipe Gesture on LinearLayout without onDown true
But keep in mind that it won't be as smooth as the navigation drawer with fragments.
As far it's not possible without using fragment. and why you don't want use fragment.?
Fragment is a best thing to achieve this.
Here i am attached some links.
I hope that links are use full for you.
https://www.learn2crack.com/2014/05/android-working-with-fragments.html
http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

It would be correct to use the NavigationDrawer to open Activities, not fragments?

My application basically has one main activity. Within it there are three tabs, each with a fragment. Something similar to the layout of whatsapp.
To add more functionality to the application, I saw that the NavigationDrawer would be a good option. But because of my application running with a main activity with children fragments, I wonder if it would be a bad practice loading activities and not fragments, when the User clicking on any item NavigationDrawer.
Or does the best meneira would turn my MainActivity in a fragment? It would give me a great job ..
I would like to suggestions =)
You can use the NavigationDrawer to open new activities. Basically just make each of the Activities have the same-looking Drawer and the user experience will be same as if you were replacing Fragments.
However, I would discourage this. I have been working with apps that mixed Fragments and Activities for the NavigationDrawer and the outcome was problematic, especially when it came to backtracking and saving state. It did work, but required hackfixing and some illogical code.
The best practice for the NavigationDrawer is to have one "container" activity which does little more that just exchanging Fragments in a FrameLayout it holds. The rest of the logic would be in the Fragments. This way the app is easily extendable and the backstack is handled by the platform.
I prefer Fragments but there is no problem to open new Activities. Instagram does something similar. It has tabs (I know that you want to use NavigationDrawer, but this is just a example), and one on these tabs open a new Activity, with the X button to close the Activity.
Instagram Main Activity:
New Activity with close button:
The Series Guide Android application does exactly the same. It opens Activities from the Navigation Drawer. Such Activities extend the BaseNavDrawerActivity. The Activity's start and exit animations are custom to make the transition looking smoother.
I would recommend this approach since it's much more easier to manage Activities' back stack than Fragments'.
Proceed happily with opening activity as well. Make sure you put the back button on the opened activity so that user would have clear idea that which screen is the primary.
But wait..
If you are worried about whether you are following the standards (industry) or not then read below:
If all your activity has the equal importance for users (including the tabbed one) then recommended way is to use the fragments and on the other side if you don't want people to get distracted by navigation drawer icon or tabs then better to open a new activity with just a back button on top..
I hope it would give you idea how to proceed..

Android - relationship between Fragment and Activity

my question is really simple: can I create/start a Fragment from an Activity, and vice versa? And how can I do that?
I need to implement this for my app, and I read discordant opinions on the web. Some say that you can only open fragments from other fragments, other say that it's possible but you shouldn't do that, instead you should open fragments only from activities. I'm a bit confused.
I found different ways to do one thing or another, but none worked. The only thing that worked for me was create an Activity from an Activity. What I need to do is to start an Activity from a Fragment, or a Fragment from a Fragment.
Before doing any kind of re-implementation I'd like to understand what of this can actually work, and why the other not.
Thanks everyone, any hint is welcome.
Fragments have been introduced since Android Honeycomb (3.0) and are a very important concept in developing and designing your apps.
To answer your questions, you can start a fragment from your activity and you can also start activities from your fragments. You can also start fragments from other fragments. You can use them to make your code much more adaptable by replacing them in tab layouts, or master-detail layouts.
There's a lot to read and understand on using fragments and implementing them into your app, so I suggest you read the Android documentation on fragments.
Fragments are placed inside activities and their life cycle is heavily tied to the life cycle of the containing activity.
As for "opening" fragments. Are you talking about adding fragments? You can add fragments to activities via code or XML and you can add fragments to fragments (nested fragments).
Launching an activity is possible either way - from fragments or from activities - via intents.
Fragment: typically represents a reusable portion of an activity's user interface, but may also represent reusable program logic. You can combine several fragment to create the user interface that make better use of your phone or tablet.
you can easily interchange fragments to make your GUI'S more dynamic.
Fragment must be hosted by an activity and they can not execute independently.
Fragment they have their own life cycle which mean they can start an app.
for example: they have onCreate() method so the fragment can add their own menu items to host an activity menu.

Show two Activities inside an Activity

I have two different Activities, and I want to show them both at the same time in another Activity. Is there any way to do that?
Thanks in advance!
its not possible , You have to use Fragments to do so...
convert those two Activities to Fragments and create a Activity and place those Fragments in Activity.
Ref :
http://developer.android.com/reference/android/app/Fragment.html
http://developer.android.com/guide/components/fragments.html
Yes, it is possible with ActivityView in android. There is no proper documentation that is available on the internet. but if you can check the android source code you can able to find the code related to ActivityView. Android is using the ActivityView for its car launcher applications.
check out below links:
google implementation of ActivityView.java
you can find the ActivityView.java usage examples
here
and here
Yes there is a way ! You cannot display 2 activity inside one, you have to use fragments !
A fragment is an independent component which can be used by an activity. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts.
A fragment runs in the context of an activity, but has its own life cycle and typically its own user interface. It is also possible to define fragments without an user interface, i.e., headless fragments.
Fragments can be dynamically or statically added to an activity.
You'll find the answers of all of your questions about fragments on this link
Hope it's been helpfull =)
Fragments and FragmentActivity comes into play here. Make your two activities separately as two different fragments, And attach these two fragments into a FragmentActivity. So that in a single screen you can view two activites(Here its a fragment).
You can also try using ViewPager concept it may suits to your scenario.
Download the sample here,
http://developer.android.com/training/animation/screen-slide.html

Have separate activity for swipe tabs

I am new to android programming and currently i am doing an app with with three tabs like this -
I followed this link to develop it.
But my current app calls separate XML files for each tabs. I want to call separate activity for each activity.
Please guide me with some materials or tutorials from basic.
I want to call seperate activity for each activity.
That is not supported. The old activities-in-tabs approach has been deprecated for over two years. You are welcome to use fragments or ordinary ViewGroups as the contents of your tabs.

Categories

Resources