If theres multiple fragments after fragment remove from backstack, which method runs - android

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
}

Related

How to pass data from Activity to fragment inside fragment

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
}

Pass data to previous fragment

I have two Fragments A and B. I am going from fragment A to Fragment B using Following method.
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new B()).addToBackStack(TAG).commit();
Now I want to send data to from fragment B to Fragment A i.e Previous Fragment.
Can you please give me an idea about how to send data to previous Fragment.
Thanks
Check the documentation about Fragments and its communication.
Do you want to pass info from one fragment to another? One fragment shouldn't know about other fragments. Make the fragment B communicate with its activity through an interface and make the activity pass the data to fragment A
You can communicate between fragments through Activity where you are replacing fragments.
Pass data to activity and then call method of that fragment where you want to update data.
2.Other way you can take a variable in Activity and save the data to this variable and when another fragment (Fragment A) is loaded(Resumed) then show data from Activity variable by getting
((HomeActivity)getActivity).variableName;
You create a subclass of Fragment called MyFragment that A, B are instances of it. Then create a static factory method take a MyFragment as param like:
public static MyFragment newInstance(MyFragment a){
MyFragment b = new MyFragment();
MyFragment b.a = a;
return b;
}
b.a is a field of MyFragment. Then you can use reference a to send data.

Call Method in Activity on change between Fragments

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) {...}

Interface from one fragment to another android

I am facing a problem implementing an interface defined in one fragment and using it to another. I know I need to do this through activity but I have added fragments dynamically inside another fragment. Please look at the snapshot to understand more about my problem.
. I have a fragment called ACTIVITY fragment inside which I load fragments dynamically. The Comments textview is clickable and when clicked it a CommentDialogFragment is shown. This dialog fragment is shared by all the fragments. Code when comment clicked:
FragmentTransaction ft = getActivity()
.getSupportFragmentManager().beginTransaction();
CommentDialog fragment = CommentDialog.newInstance(id,
"Activity");
ft.addToBackStack(null);
fragment.show(ft, null);
I want to increment the comment count. For that I made an interface:
public interface IncrementComment {
public void increaseCommentCount(boolean increase);
}
I am unable to use this interface in my fragment inside the ACTIVITY fragment. The interface is detected on the Activity that holds all these fragments. Heres the interface in my MainActivity class I get the data upto this point:
#Override
public void increaseCommentCount(boolean increase) {
// TODO Auto-generated method stub
Log.d("interface main activity", "called");//this is called
}
Now I am unable to pass data from this activity code to my fragment because the fragments are loaded dynamically and there can be any number of fragments(user can see their old post).
So I tried to make a fragment implement the interface to update the value of the textview. But I couldnot get it working as it is never called. Can someone point me in the correct direction. I tried most of the links in SO like this and from other sources like this but none of them fit my requirement.

Headless Fragment Issue

I am trying to retain state of the multiple fragments in my project. Their parent is a single Activity and i need to implement the TaskFragment in all the Fragments but not the Main Activity.
In the worker-fragment example, an Activity is implementing the TaskCallback which works but how will it work with a fragment ?
https://github.com/alexjlockwood/worker-fragments
I am trying to implement TaskCallbacks in my fragments such that the interface's overriden method call goes directly to the fragment like this :-
public class MyFragment implements TaskFragment.TaskCallbacks{
//Some Code
//Code for initializing TaskFragment
//Overriden method of Task Callback
}
However, when i do this, it is showing an Ilegal state exception in onAttach method of TaskFragment that Activity is not implementing callback interface.
How do i get the instance of the fragment which is implementing this Task Callback ?

Categories

Resources