Show two Activities inside an Activity - android

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

Related

Fragment vs. Activity in specific scenario

I have an android app where I want to have different functionalities, like a search function, a history, user defined lists,... Each of these functionalities is accessible via NavigationDrawer, and I want to contain a ViewPager to allow navigation in Tabs in each of those specific functionalities. Finally, when I switch between functions via NavDrawer I want my app to restore the state the corresponding function was in the last time it was used.
I'm puzzled about the best way to implement this. Currently, I'm using one Activity, which contains the logic for functionality navigation, and saving and restoring of current state of each fragment. I'm using one ViewPagerAdapter to hold information about current active fragments.
Another possible approach would be to make an Activity for each top-level functionality, and then implement the Fragments as needed. A third approach I can think of would be still using only one Activity, but different ViewPagerAdapter to hold the state information of each top-level function.
I'm really not sure which is the best way to go here, and I would like to settle on one of those possible options before going further, but I can't really find any useful information on the net. Are there any Best Practices known for my given scenario, or is this really just an opinion based topic?
Edit: No duplicate of Difference between Activity and FragmentActivity because I'm not even tackling the issue of support library vs. native
Edit2: I think it should be also possible by using nested fragments, i.e. having a fragment which hold my ViewPager where I could add my fragments for that function. But I really would prefer not using nested fragments at all.
A Fragment is best thought of as "a group of reusable interface elements". So if you can think of reusable elements across the activities (or a single activity when it's recreated) then you use fragments.
Activity is best thought of as "a single screen with UI". So then..

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

Some questions on when to use fragments and activities

Just a disclaimer, I am pretty new to Android and slowly working through tutorials. Most tutorials dont talk about fragments at all in the beginning, but Android-studio by default sets up one for you.
I've read some of the past questions and the dev blog related to fragments and activities and they were helpful in giving me an idea as to the advantages of using fragments.
I am still a bit confused on when one would use a new activity in an app, it seems to me like everything could be accomplished with fragments and a single activity.
Lets say an app has multiple screens, do you implement that as one activity with multiple fragments, or multiple activities with one fragment each to them.
This image makes sense to me and demonstrates the power of fragments, but why on the handsets example is two activities required?
Another add-on question, should everything moving forward be done in a fragment?
Thank you and sorry if these questions didnt really make sense.
An Activity should be the host for a collection of related Fragments. For instance, you might have something like:
Base Activity extends FragmentActivity
LoginActivity extends BaseActivity
-- LoginFragment
-- LoginErrorFragment
-- LoginSignUpFragment
SettingsActivity extends BaseActivity
-- SettingsGeneralFragment
-- SettingsAdvancedFragment
If you try to move all of your logic into a single Activity, it's going to get unmaintainable very quickly. Another good practice is to have a base Activity which all of your Activities extend; since if you suddenly find that there's some functionality you want to provide to all activities, you can just add it to the base class.
Everything said above in both regards are absolutely correct. I would just like to add few points to them.
When thinking about fragments please keep in mind that they are a part of an Activity, which like any other view can be added, modified and replaced dynamically. For example, while using ActionBar's and Navigation Drawers fragments become more handy and flexible. Similar things stands true for ViewPager etc.
Fragments also cater to larger screen sizes in a much better way than the traditional Activity approach. Imagine the users experience then when for every action performed a new screen would replace the Phone/ Tablet against now when all the actions and their performed events lie on the same screen.
One more thing which I like about fragments is, we dont have to declare them in the Manifest. :) Most of the time we forget to do that with Activities until the compiler prompts. :) (At least me)
As you said. In simple application you can use only one Activity and just replace fragments. I did it in my apps and it works perfect. Sometimes you just need to start new Activity if you want to follow android design and architecture patterns.
According to your question about images that you posted you can
obtain the same effects using just one Activity and Fragments.
Yes everything moving forward can be done in a Fragment.

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.

Categories

Resources