I am trying to pass a view from one fragment activity to another activity. I am a newbie at Fragments and am not sure if this is possible.
Any thoughts are gladly appreciated.
Thanks.
You don't want to do this, the view will be tied to the activity.
Why do you want to do this, or more specifically what is it you want to achieve?
You can pass information from one activity to another via Intent Extras if you need to recreate the view based on information.
Related
We use intents to switch between two activities and also fragments are for the same purpose. So why can't we use intents always instead of fragments?
Intent, you can think of intention to do some work. It can be either go from one activity to another activity, send email, open some links and so on.
Fragment is just like part of those activities.
To make it simple you can think of activities as full website page whereas fragment as a part of that website page. So, activitycan contain any number of fragments.
I think :
1.In the changing fragment you just change part of activity and not whole of them , but by intent you change whole of activity to other.
2.by intent you can communicate between android components such as activities, content providers, broadcast receivers and services, but by fragment you cant and in the otherwise fragment is child of activity.
I am having 2 activities, in each activity I am having a view-flipper and button. When the button in 1st activity is clicked, I want to view the 2nd activity's view-flipper child. How to do that? Is that Possible? If yes, please tell me how to do that.
Thanks in advance.
You cannot directly interact with views between activities. Instead, you should pass data in an Intent when you call startActivity() from the first activity. The data should tell the second activity which view to start with. For example, you can send the name or index of the view. You can learn more about sending data in an Intent from the official Android documentation.
I need a way to share Fragments through different activities, so for example I have MainActivity with a Fragment and when I go to SecondActivity I need that same Fragment loaded, but the Fragment can vary so it would not always be the same one.
I've guessed that I could get the actual Fragments id or tag and pass it on the Intent so I could retrieve it on SecondActivity and use it to load the correct Fragment, but I don't know how.
You can't. You have to create a new instance for the fragment and load again the data in it. If you created the fragment in a good way, it is a really simple task. The reasons why you can't reuse the fragment are the following:
If you could do something like that, what would happend to this fragment's lifecycle? What about going back from current activity to the other? Everything can be messed up.
Every activity has its own FragmentManager and they are never shared between activities, so you can't ask in another's activity for a fragment that doesn't belongs to it.
If you have some doubts on how to pass data using intents between activities to tell the fragment what to load, have a look at this post.
first of, I know that it is not possible to start a fragment via an Intent like you do with activities. However, in my application I would like to have the functionality that I can return from the activity to a fragment on a button click.
How do I do that? Any suggestions?
You can't "return from an Activity to a Fragment", simply because Fragments need to be put somewhere. Which means that hey need an Activity as a container. Putting aside some dirty overlay tricks.
So in the usual cases, you need an Activity to hold your Fragments. And since you have an Activity, you can have an intent-filter to handle your intent.
Activity hosts fragment, I think you should return to an Activity and select the correct Fragment. But there's always problem if you wanna select a Fragment freely, because you might have had a Fragment-Stack. If you don't have, ignore what I wrote below.
You have maybe three chance to reach your points. If you've called addBackStack, by default the Android use a backstack to control your Fragments, you have no chance to select your Fragments freely. so....
Chance 1 maybe worse case: Do not use addBackStack, and always use replace to finish your fragment's transaction.
Chance 2: Use FragmentTabHost. Then you can free select your fragments you've created, but be care of their life-cycles.
Chance 3: Use ViewPager, u know it ! :)
i need to know whether transaction style is possible or not in android , for example if i launch the main activity i need to go or switch over to second activity ,i need some animation like popup , pop for left..etc , for next activity view. whether it is possible or not?
Use Intend to go another activity.
Intent intent = new Intent(SourceActivity.this, TagetActivity.class);
startActivity(intent);
Further to understand about intend:
http://www.castlerockresearch.in/dev/2010/08/understanding-intents-and-intent-filters-in-android/
For Pop up animation see this link:
http://android-codes-examples.blogspot.com/2011/04/animated-customized-popup-transparent.html
you could use Intents for switching between 2 activities.
For the animation part please look at this How to provide animation when calling another activity in Android?
This could also be useful:
anddev.org/animation_on_new_intent-t1207.html
BR,
mybecks