I'm very new to Android development.
I've made some researches to achieve passing data from Fragment A in Activity A to Fragment B in Activity B.
Is that possible?
All I could get from the tutorials on the internet is that we have to create an Interface that will be implemented by the parent Activity of two sibling fragments. How to achieve it when the fragments belong to two different activities? I'm stuck.
Thanks
You would achieve it by implementing the said Interface and put the extras into the Intent for the Activity that has the other Fragment.
Pseudo code could look like this
Fragment A - calls interface methods on Activity A and passes data
Activity A - calls startActivity with the extras inside the Intent
Activity B - got started and evaluates the said Intent and passes it to Fragment B
Fragment B - receives the extras from Activity B
you can use Intent to pass data between activity and from sibling fragment call method of activity like ((MainActivity).getActivity).methodSentData(String exampledata) and ((MainActivity).getActivity).methodGetData(); to get data in the fragment.
methodSentData implements intent.putExtra("data",data); to sent data to other activity and methodGetData() implements getintent().GetStringExtras("data");
You can use local broadcast receiver.
Related
I have a fragment, and I am starting an activity from the fragment. Now I want to call a method from the fragment in the new activity.
I tried to use interface but it seems I can't since I don't create an object of new activity in the fragment to have it call the setListener(). I am using intent to fire up the new activity.
I am not able to find how I can get fragment instance in new activity or how to call a method in the fragment. Any help would be great!
A fragment is tightly coupled with the Activity. You always need to create the Activity as the host for the fragment.
From the documentation:
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
So, you need to do the communication between Activities. An activity should not communicate with a fragment it didn't host. But it should communicate with the Activity where the fragment is hosted.
For example,
if you have two Activity which are ActivityOne and ActivityTwo. Where ActivityOne has a Fragment called ActivityOneFragment.
When you need to get the ActivityOneFragment from ActivityTwo, you need to communicate with the ActivityOne then tell it to get the ActivityOneFragment:
ActivityTwo -> ActivityOne -> ActivityOneFragment
You shouldn't do this:
ActivityTwo -> ActivityOneFragment
No, you can do not that. Because the background activity is paused/ dead. so you can not access its method.
if it is general method, you can put that method in other class. call it your utility class.
I have 3 activities which I want to convert into fragments so that I can implement a ViewPager to swipe between them:
Activity A ( to the left )
Activity B (central)
Activity C (to the right)
Activity B is called from an external activity and data is passed to it within the Intent. Now Activity B passes data within intents to Activities A and C when they are launched.The idea here is that B displays a slideshow with an ImageView which is updated every X seconds and A and C display information about the individual pictures. Hence, here are my questions:
How do I replicate this functionality with fragments so that when I swipe from Fragment B to C and from Fragment B to A I can pass the appropriate data to them. I currently do so this way with my activities:
Intent a = new Intent(SlideShow.this,MapsActivity.class);
a.putExtra("long", longitude);
a.putExtra("lang", langitude);
pauseSlideShow(i, timeRemaining);
isPaused = true;
startActivity(a)
I read up on the differences between FragmentPagerAdapter and FragmentStatePagerAdapter. Currently, when I launch activities A and C from B I do not call finish() on B so that when I return to it, it is in the exact same state as before. However, since activities A and C are supporting B, when I go back to activity B from A or C I end them by calling finish() on A and C respectively. How can I do this with fragments?
If you are using FragmentPagerAdapter, make sure that when you create a fragment in getItem() you save a reference to the fragment somewhere in the adapter or (preferably) the activity.
Create a method (i.e. getLatLong()) on fragment B that returns the data.
In fragment A in onCreateView(), you would get the reference to fragment B from the activity and call getLatLong() to get the data you need.
You can keep your fragments available in memory by calling viewpager.setOffscreenPageLimit(2). This way fragment A won't be destroyed when you swipe to fragment C, because the ViewPager will keep two fragments to the left of fragment C, which are your fragments A and B.
Another (probably better) approach is to create all three fragments at the beginning of the activity and return the appropriate fragment when getItem() is called. By doing this, you can use setTargetFragment() and getTargetFragment() to allow the fragments to get data from one another. When you do this, remember that a fragment may or may not have a UI available, so if you are pushing data rather than pulling, make sure the method receiving the data checks for getView() == null.
I have the following situation: I open the Fragment A and goals, through a click event of the button, go to the Fragment B. When I am in Fragment B and hit the back button (in order to return to the Fragment A) I would like to pass some parameters to Fragment A. Does anyone know how I can do this?
Thank you in advance to all.
Your activity can implement a custom listener interface on the Activity and use a reference to it in your fragment to pass the communication back.
You can send a broadcast when you hit the back button in Fragment B to Fragment A with the parameters you need put in the bundle, and have broadcast receiver on the Fragment A.
Hope this helps!
I'm trying to open a new fragment based on a button push in a previous fragment. What's the best way to implement this?
I'm curious if it's Activity -> .add + .commit original fragment - > from that fragment.java .replace new fragment?
Or do I need to pass an intent back up to the activity and create/replace that fragment from the activity?
So summarize: Activity A - > Fragment 1 - > Fragment 2.
I'm also slightly confused on what things I [need] to #Override. I think only onCreate and onCreateView [within each fragment]?
I'm only looking for high-level here; I want to struggle through the code myself.
Fragments are generally unaware of their host so I would use the standard callback method to call your activity and ask it to switch fragments.
Create an interface
Have your activity implement the interface.
Cast the getActivity() call to your interface.
Call the interface method.
This is much cleaner than casting the host activity and calling methods on it. It also means your fragment can be hosted in different activities with no cast errors.
http://developer.android.com/training/basics/fragments/communicating.html
You need to cast getActivity like:
((MyActivity) getActivity())
And then if you have that, you can call a method in yout activity:
((MyActivity) getActivity()).replaceFragments(Object... params);
And inside the method you should do the replace fragment process.
So simply you have the right idea.
I have a several activities in my app. The main activity (activity1) extends ActivityGroup (I need to support 2.2 and above, so I cant use fragments).
The main activity1 creates activity2, which in turn creates activity3. But when creating activity3 I want activity1 to create it, not activity2.
How do you pass the main activity around between activities?
Thanks
A better way is to use startActivityForResult() in avtivity1 to statrt activity2.
and when you want activity3 just finish activity2 and in activity1 override
onActivityResult() and start Activity3. passing activity instance is not better idea
since that Activity may get killed when at background and thus susceptible to throw an
Exception.