What i have
I have an activity
MainActivity.java here in this activity I have a
fragment(MainFragment.java)
In MainFragment.java I have two child fragments ChildOne.java and
ChildTwo.java
I have a editText in ChildOne.java
What I am trying to do:
I need to get the value in the editText of ChildOne.java from
ChildTwo.java
how to achieve this !
You can achieve this in multiple ways, but I'll give you one supposing that both Fragments are displayed at the same time.
In your ChildTwo use getParentFragment to get MainFragment.
MainFragment parent = (MainFragment) getParentFragment();
String edit_text_value = parent.getChildOneText();
In your MainFragment you need a reference object to your ChildOne fragment.
public String getChildOneText()
{
return mFirstFragment.getEditTextValue();
}
Finally in your ChildOne fragment create a method to return your EditText.
public String getEditTextValue()
{
return my_edit.getText().toString();
}
Hope it helps!
You may create one function in mainActivity and a variable that holds the value in there. And call that function when you want to save the data of editText, And can create a function to get the data in MainActivity as well. Call those functions like this -
((Home) getActivity()).shareData("example string");
((Home) getActivity()).receiveData("example string");
May check this answer and this one
Related
I'm an Android novice.
As with the title, I want to convey some data from Parents Fragment to child Fragment.
But I'm stuck in it and very hard.
Maybe try saving your child Fragment as a variable in your parent Pragment. Then you are able to call public methods like in your case childFragment.setDate(date).
To transfer information from the parent fragment to the child create an interface
interface YourListner{
void methodToPassData(Date date);
}
After which the child fragment should implement this interface
Then you can cast the fragment to the type of the interface and pass the date to it by calling the method.
ChildFragment fragment = new ChuldFragment();
//display fragment
YourListner fragmentListener = (YourListner)fragmentListener;
fragmentListener.methodToPassData(date);
You can also do this using the constructor in the child fragment.
I have a tab view with two fragments. Those two fragments contain a recycler view with cards.
Each card in both fragments had a button.
Clicking on fragment 1's button should open the fragment 2 as a separate page and vice-versa.
I am struggling to find a method to implement this without making every too complex and tightly coupled.
This is fragment one with its own Adapter.
And this is fragment two:
Clicking on that SELECT DONOR button in Donees page should open donor fragment in a new page where the user will be able to assign a donor for the selected donee.
So I have two needs here
1) To start a fragment from a fragment
2) To Keep track from which Donee the new donor page was opened so that I can assign a donor for that specific donee.
I hope this is understandable.
so far I have tried LocalBroadcast and FragmentManager but its hard to keep track of what I'm doing with the code.
Can you guys suggest a better technique to achieve this ?
the easiest solution would probably be, starting a new activity, passing something like an ID, name or something to the intent on an Button click.
Context.startActivity(new Intent(Context, YourAssigneeActivity.class)
.putExtra("ID",id));
So I assume that you do not switch to the other tab when you click a button on one tab. Therefore the fragment should fill the whole screen.
With this assumption in mind you most likely have to switch the Activity. This can be dones easily with an Intent:
Intent intent = new Intent(getActivity(), ActivityB.class)
intent.putExtra("KEY", <your required data to transfer>);
getActivity().startActivityForResult(intent);
Note that when you use putExtra() don't forget that you need to implement Parcelable in those objects (explained here)
To get to know which item was clicked you can use the following pattern (pseudocode - I personally think it's really clean):
FragmentA implements YourAdapter.callback {
onItemClicked(<YourObject> item) {
<starting new activity as described above>
}
}
class YourAdapter extends RecyclerView.Adapter {
Callback mCallback;
YourAdapter(Context context, otherStuff) {
mCallback = (Callback) context;
}
interface Callback {
onItemClicked(<YourObject> item)
}
YourViewHolder implements View.OnClickListener {
onClick(View v) {
mCallback.onItemClicked(<YourObject> item)
}
}
}
Once you are in your Activity, you can set the Fragment in onCreate() of your Activity. In the Activity retrieve the data with getIntent() in the onCreate before creating the Fragment. Then you can put your data in the Fragment with setArguments(<Bundle>). In the Fragment in the onCreateview() retrieve it with getArguments().
I know this is kind of conmplicated. A better solution would be to just switch to an Activity and forget about the Fragment. This would remove one layer of complexity.
If you directly go from Fragment to Fragment you can ignore the Activity part but the rest should stay the same.
Hope this was the answer you were looking for!
Edit: Note that mCallback = (Callback) context is null if the Activity is not implementing Callback
I have this kind of structure
MainPage Activity1 has view pager holding 3 fragments
I am using tabapdater
The first of the fragments have method to move to another Activity2
I want to fire some method in the first fragment when button is clicked in Activity2
Can i do that? can you guys give me any example code for it??
Thanks guys
There are some solutions.
The first one I think is startActivityForResult. If you start your second activity by calling startActivityForResult instead of startActivity you can expect the result in first activity by overriding the onActivityResult method and then change the first fragment content from there.
Another good and easy solution is using Buses like EventBus and otto.
They are well documented.
In your Fragment class:
public void foo(Object objectToPassFromActivity) {
// do what you want
}
In Your activity:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentByTag(getFragmentTag(R.id.yourViewPagerId, fragmentPosition);
fragment.foo(objectToPassFromActivity)
Add method:
private String getFragmentTag(int viewPagerId, int fragmentPosition) {
return "android:switcher:" + viewPagerId + ":" + fragmentPosition;
}
You can see the link that i added in comments to get more details.
Scenario is: I have two fragments FragmentA and FragmentB.
In FragmentA, I have a simple form and in FragmentB, I am showing data of that form in ListView.
I am getting data in listview but not in real time. Like first I insert data, then, I restart Application to see data in list.
My question is how to notify change from one fragment to another in real time.
you can use listener in your activity and then notify:
class MyActivity extends Activity {
FragmentA fragA;
FragmentB fragB;
public void notifyB() {
fragB.notifyB();
}
class FragmentA extends Fragment{
private void notifyB(){
((MyActivity) getActivity()).notifyB();
}
class FragmentB extends Fragment{
public notifyB() {
// do something
}
}
After setting the variable of the FragmentTransaction you can do like this:
Bundle bundle = new Bundle();
bundle.putString("flag", variable)// You can pass Strings, Int, Boolean.
//The "flag" is the key to get the value in the other fragment.
//The variable is wat you need to pass to the other fragment.
In you new fragment you can get your viable like this:
Bundle bundle = new Bundle();
String a = bundle.getString("flag");
// now you have te value in the variable a
this works in eclipse
Could somebody help me to create my own Listener.
I have an Activity where a String is changing and in an Activity's fragment I have to set a TextView's text for this String.
Why you are using Activity and Activity fragment?
just use fragment and make an interface
public interface ddd
{
void MyTestInterface(String str);
}
make your activityFragment implement it and than just pass your activity (this) to the other fragment how handle the string changes.
like: MyTestInterface.stringCallback("str")