Have separate activity for swipe tabs - android

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.

Related

Android Studio- starts with 2 fragments

I searched through the site but could not find an answer so here is my question.
I am using Android Studio 3.6 and when I start a new project and choose Basic Activity there are two fragments appearing in layout. So in layout there are .xml files activity_main,content_main,fragment_first and fragment_second. Therefore, when I start a new project, I get a different screen than the usual "hello world" screen. To add, I cannot basically edit the fragments. So, how can I back to "normal" and start without these fragments?
When you create a basic activity, you basically create an activity with two fragments. this means that basic activity by default provides two fragments that you can edit and use. basically, it's almost the same with bottom navigation activity. the difference is only in basic activity there is no bottom navigation.
if you want to make one activity app. you don't really need to use fragments. so you can just make a Empty activity instead of basic activity. if you want to make your activity fragmented in the future, you can add it later.

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.

Activity or Fragment?

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

Do fragments mean I should code single-activity apps?

OK, I get the concept of Fragments being modular/ interchangeable "sub-activities" but given that fragments have their own lifecycle much like the one of an activity and can pretty much do anything that an activity can, from a best-practice standpoint, does that mean we are to start coding a single activity for the whole app (say, the main_activity) and simply adding/ replacing/ removing fragments within that single activity?
If not, then how do I know when it's time to create a new activity instead of continuing to push new fragments into the same old activity?
Just trying to understand how to best organize my app's features into activities and frags. A practical example would help (no code necessary, just the concepts).
Cheers,
No, I does not mean that you should use single activity.
Fragments are to help you organize ui elements (especially on big screens (like tablets)).
They also introduce new layer of reusability (using fragments for loaders, simple views) across your projects.
I recomend you check Google NewsReader SDK example which is a great way to see how to implement activity/fragment patterns depending on what type of screen it is launched on
http://developer.android.com/training/multiscreen/adaptui.html
Download sample button is on the right
No fragments are just one way to reuse parts of an application. For instance you can use a fragment in several activities. On the other hand you can of course have several activities that use different fragments or do not use fragments at all.
So summary: no you don't need to. Fragments are helpful, if you build up multiple activities that use a similar or equal component as a part of their layout.

Android Layout Fragment/Activity Confusion

I am making my first android application with the ActionBarSherlock.
The application will always have an action bar consisting of 3 tabs (first tab selected by default).
The app could be extended for use with a tablet.
I have been searching the web, and following the android development guides, however I am finding a few things confusing.
The first tab screen will be a list view with a list of items, onitemselected should send the user to a screen which features more details about that item.
When should I use a fragment? Should each tab be a fragment?
Or, should each tab call a new activity, which consists of fragments?
And, if using fragments, should I place them in different classes, or embed them within an activity??
Appreciate any help, thanks.
you should probably read these two links first.
http://android-developers.blogspot.com/2011/09/preparing-for-handsets.html
http://android-developers.blogspot.com/2011/02/android-30-fragments-api.html
If you plan to make an app that work on both phone and tablet. It is a good idea to use a fragment, and then use a shell activity to wrap that fragment.
My experience with Fragments is mostly on ViewPager, so I am not entirely sure if it applies here.
In Android, you should use Fragments as much as possible. As a general rule of thumb, imagine you are translating the UI from phones to tablets, elements that can stay together in the same configuration should be a Fragment.
There is a Fragment subclass called ListFragment, so you might want to look into that for your first tab. (ListFragment is for Fragment like ListActivity is for Activity)
There is also a tutorial I found to deal with fragments. Did not really look into it but I hope it helps.
http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
As for ActionBar / ActionBarSherlock, I have absolutely no experience withit so someone might want to add to that.

Categories

Resources