Headless Fragment Issue - android

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 ?

Related

How to pass a Fragment as a callback to DialogFragment using Navigation Architecture Component?

I have a ParentFragment that implements a callback Interface.
From that fragment I call a DialogFragment (using childFrgamentManger) and passing ParentFragment as a parameter. Pressing a button on DialogFragment calls a ParentFragment's method that updates the ParentFragment.
Now, I am trying to implement that using Navigation Architecture and I can't figure out how to pass the ParentFragment as a parameter to DialogFragment.
Most examples I saw pass either Strings or Integers.

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

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

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
}

How Fragment implement interfaceListener android

I have one activity which contains buttons if I click any button according to that in my fragment layout changes should happen.
for this I created a interface it has one method public void onclick();
fragment implementing interface it got onclick method. I created reference to interface in activity I am calling this method from activity but not calling onclick in fragment plz help

How to make call backs between Fragments of the same Ativity

I have 4 tabs in an Activity.
Each of them is a Fragment. And every Fragment has a ListView.
So, if i change the ListView in Fragment, it must change the ListView in all other Fragments ie.., Tabs.
The problem i face is while creating the interface instance.
It takes it's own onClick() method.
In case i want a callback to the parent activity i could have done that by overriding onAttach. But how to make a callback to a Fragment?
From Developers site:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
So, make a callback to the Activity which in turn makes a callback to other fragments??
Thank You
It's pretty simple,all You need is steps below:
1) From onClick method in your first fragment make a function call of activity:
((IYourActivityInterface) getActivty()).activityMethod();
2) In your activity find fragment by tag or id and run it's method:
public void activityMethod(){
Fragment tabFragment = getFragmentManager().findFragmentByTag("second_fragment");
// or Fragment tabFragment = getFragmentManager().findFragmentById(R.id.frag);
if (tabFragment!=null){
((IFragmentInterface) tabFragment).fragmentMethod();
}
}
Hope this is what you are looking for.)

Categories

Resources