I have an Activity ActivityA, and a fragment FragOne. I also have a button that replace FragOne to FragTwo.
I wanna call method in ActivityA when the fragment changes from FragOne to FragTwo or vice-versa.
In my problem, I want FragOne and FragTwo to be independent from ActivityA.
I think of something using life cycle from Activity.
Some idea?
From Fragment call ((ActivityA) getActivity()).yourMethod();
Also, check if getActivity() != null before calling yourMethod()
Create a interface class. Implement this interface in Activity class. Attach this interface in both fragments. In "Fragment A", on clicking button, call interface method.
In Activity, the overridden method will achieve the requires functionality(Switching fragment in this case).
You can achieve same vice versa, ie, calling activity method and getting into "Fragment A", from "Fragment B".
So,
I use this in activity:
public void onAttachFragment(Fragment fragment) {...}
Related
I have a Activity which contains a fragment with recyclerview.
The recyclerview has few editable fields and need to put validations on clicking a button on activity.
Please let me know the solution or any techniques to overcome this problem statement
use this (click here ) functionality for data sharing between Fragment to fragment , Fragment to Activity and Activity to Activity
1) in your onCreate declare the BUSand register it
Bus bus = new Bus();
bus.register(this);
2) send data or any action using
bus.post(<your data or any Item>);
3) don't forgot to unregister in your onestroy
bus.unregister(this)
You can get that fragment instance either by 'ID' or by tag (you would need to give a tag while adding this fragment).
Now you can call any public method of that fragment using its instance.
For Example (Using tag), if you want to call a public method 'show()' of FragmentA. Then while adding it to the activity give it a tag like this:
Fragment fragmentA = new FragmentA();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container,fragmentA,"YOUR_TARGET_FRAGMENT_TAG").commit();
And to get its instance (on a button click or as required):
FragmentA fragmentA = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
fragmentA.show();
Declare in your fragment the RecyclerView as public
Declare in your activity the fragment as variable
in your activity on click listener get the recyclerview with
fragment.recyclerview
I have an Activity. It adds Fragment A dynamically. Fragment A contains static added (by xml) child Fragment B. How can I pass data from Activity to Fragment B when i add Fragment A to Activity?
I know next approach:
Implement interface getter in Activity and call getter method from Fragment B
I'd like to know, are there another ways to do it?
I tried set arguments from Activity to Fragment A, find Fragment B by child fragment manager and set arguments from Fragment A to Fragment B.
But Fragment B created earlier than Fragment A, so i had exceptions..
Well there are more than one ways to accomplish that. one of the way is
Create a function public void setData(Data data) in your Fragment B
Transfer your data from Activity to Fragment A. Since your Fragment B is added by fragment tag in xml. create ref to your Fragment B object in Fragment A. Now you have the reference to Fragment B in A.
Now Simply call the function (created in Fragment B) in Fragment A and pass the data which you have received by the Activity
Other way..
In Fragment B you can call getActivity() and type cast it in to your Activity class type and call its method which can return the desired data
This sample by google should resolve all your Issues:
https://developer.android.com/training/basics/fragments/communicating.html
You can try EventBus to pass data from Activity to your Fragment B. In your activity call this EventBus.getDefault().post(new MessageEvent());
And then you just need to register EventBus in onCreateView of Fragment B
EventBus.getDefault().register(this);
And then
#Subscribe
public void onEvent(MessageEvent event){
// DO SOMETHING
}
If there's multiple fragments one on another, after one fragment remove from the stack, which method runs in the below fragment.
This is whats happens in the program,
Fragment A calls -> Fragment B
Then I remove Fragment B. So now its in the fragment A. I in that moment I want to run a method in Fragment A.
I tried onResume in Fragment A. But its not working. Can anyone please suggest a method to achieve this approach.
You can either use Callbacks to communicate between fragments, or use broadcast receivers
To use callbacks...
In Fragment B declare an interface like so
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void myMethod();
}
Also in fragment B create a global instance of our OnFragmentInteractionListener :
private OnFragmentInteractionListener mListener;
Then whenever you want the method in Fragment A to be called just use :
mListener.myMethod();
Now in Fragment A we need to provide the myMethod and implement our listener from Fragment B :
public class FragmentA extends Fragment implements FragmentB.OnFragmentInteractionListener
And also in Fragment A provide the method
public void myMethod(){
//Hello World
}
I am new at developing Android App.I am try to use a dynamic fragment which is used 2 activity like (show detail activity and edit details activity).How can I understand the fragment is used by which activity in onActivityCreated() method on MyFragment class. How can I handle this issue please help me thanks in advance
Try this in your Fragment onActivityCreated() method
FragmentActivity activity = getActivity();
if(activity instanceof show detail activity){
// Your ShowDetailsActivity
}else if(activity instanceof edit details activity){
// Your EditDetailsActivity
}
Jagadesh's solution will work, but fragments should operate independent of the activity.
You might want to consider adding a static method like "getInstance" that will accept parameters and return an appropriate instance of the fragment if some functionality shoudl be custom for the calling activity.
If you want to call the activity back, then you might think about having the activities register a callback or listener that the fragment can invoke.
Based on the example from http://developer.android.com/training/basics/fragments/communicating.html I tried to reproduce the communication between two fragments which are sub fragments of a larger fragment.
In the example, AB activity contains A fragment and B fragment. But I am trying to achieve the same but in my case AB Fragment contains A fragment and B fragment.
The problem is the overridden method in the AB Fragment never gets called. Does this not work because the containing component is a Fragment and not a Activity like in the example? Am I missing out something here?
If you are referring to onClick() or some other onSomething() handler, then these always get called in the Activity class, not the fragment. So in the example you linked, the onArticleSelected() must remain in the Activity, even if you have nested fragments.
To pass info on to the fragment, you have a few options. One, you can keep a reference to the fragment within the activity. This might be lost if your activity recreates (settings event for example).
The second and better way would be to tag your fragments, and then use findFragmentByTag.
When you add your fragment (notice the parameter "my_fragment" which is the tag I gave to the fragment):
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, myFragment, "my_fragment").commit();
Or when you replace one fragment with another:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment, "my_fragment").comit();
Then, when you want to do something in the fragment from within your onArticleSelected of the activity:
Fragment fragment = getSupportFragmentManger().findFragmentByTag("my_fragment");
if (fragment != null) {
fragment.articleSelected(articleId);
}
You can always use an Interface to communicate between fragments. It is the safest way to do so.