How do I update my EditText of a fragment from another fragment? - android

I have two fragments, in which on first fragment there is 5 edittext box and in second fragment there is 3 edittext box and one button . On clicking of fragment two button , I want to generate alert message on 5 edittext box which are in first fragment .
Can anyone help how can i achieve this ??
Early reply is appreciable

You can get the second fragment by tag using fragment manager, and then do whatever you want to its views. be careful of NPEs though.
First, when you call the second fragment you should set a tag:
SecondFragment fragment = SecondFragment.newInstance();
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragments_container, fragment, "second_fragment_tag")
.addToBackStack(null)
.commit();
Then, you can access it from the first fragment using the following:
SecondFragment fragment = (SecondFragment)getFragmentManager().findFragmentByTag("second_fragment_tag");

Related

How to get top Fragment in backstack from DialogFragment

I'm using navigation to navigate between fragments.
And I have a DialogFragment, it can called from from many fragment like this:
val dialog = FragmentDialog
dialog.show(childFragmentManager, "home_fragment")
And I want to know dialogfragment called by which fragment
I tried with FragmentManager.backStackEntryCount but it's seem doesn't work
Can I have a advice for this problem ???
There are multiple ways to do this ..
You can just Pass a key in Bundle to your DialogFragment to check which Fragment opened it ..
If your Dialog fragment a attached to a fragment in each case you can just use getParentFragment() . getParentFragment() will return the instance of that fragment. If its attached to activity then getParentFragment() will be null so watch out for this case also .
you can also use getTag() from where it was open byt passing different tags to transaction .

Android. How to avoid fragment be recreated when back from another fragment?

I'm using MVP in my project. I have an activity. Inside this activity I'm showing fragment. Fragment contains some data, edit text, buttons etc. From this fragment it is possible to navigate to another fragment. Here is my code for showing another fragment:
getParentFragmentManager()
.beginTransaction()
.replace(R.id.main_container, secondFragment)
.addToBackStack(null)
.commitAllowingStateLoss();
Next, when I try to go back from fragment 2 to fragment 1, my fragment one is re-created.The method onViewCreated() is called again with saveedInstanceState = null. Also in the onViewCreated() I call presenter.onCreate(savedInstanceState) and because of this, all requests that should be called when I first enter the fragment are re-called when I back to first fragment.
As far as I understand, when calling a .replace(container, fragment), I cannot avoid recreating the fragment view, but in this case, how can I save all my data in the presenter so that I do not re-execute all requests when returning to the fragment?
P.S. With .add() fragment is not recreated. But in my activity I have toolbar with title, and I don't need it to show in my second fragment. When I open my second fragment with .replace() toolbar is not showing, but when open with .add() toolbar is showing.
Use Fragment Result API :
For implemention this method set - setFragmentResultListener - in source fragment and also set - setResult() and finish() - in destination fragment
Note 1 : for having clean code i suggest you to use FragmentTransaction class in:
https://github.com/mahditavakoli1312/FragmentTransaction---mahdi-tavakoli
Note 2 : use Navigation for navigate betwin fragments and activities Instead tranastion

Previous fragment edittext focus issue

I am using multiple fragments.
I am adding fragment over fragment as below
supportfragmentmanager
.beginTransaction()
.add(R.id.container_login, newFragment, newFragment.javaClass.simpleName)
.addToBackStack(newFragment.javaClass.simpleName)
.commitAllowingStateLoss()
Now the issue is, despite of adding new fragment, previous fragment is not losing focus.
Typing in edittext of current fragment types in previous fragment edditext.
Even action next also loses focus in current fragment and moves cursor in previous fragment.
Kindly help.
The fragment does not lose focus because you use .add method which adds the new fragment over the one which already exists in the container.
Use .replace() method which replaces the existing fragment from the container. This is similar to calling remove(Fragment) and then use .add() method.
If you want to add a fragment in one container is impossible but if you want to replace the fragment with another fragment is possible to do it, because one layout is for 1 fragment, not more than one, so you just need to replace the previous fragment with another fragment
I have a simple code if you want to replace the previous fragment with the new fragment
First, you need to add one method with the parameter of the fragment
fun openFragment(fragment: Fragment?) {
val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container_content, fragment!!)
transaction.commit()
}
If you want to replace the fragment with another fragment just call the method like this openFragment(FragmentClass.newinstance()) soo the previous fragment will be replaced with the new fragment
I hope this code can help you to solve your problem

Android find fragment returns null always

I have an activity within it I do
FragmentTransaction trans = getFragmentManager().beginTransaction();
FragmentList fragList = new FragmentList();
trans.replace(R.id.fragList, fragList, "fragList");
to fill my frame layout with the actual fragment. That works fine. Through navigation I end up later in some other activity and some other fragment. In that new fragment I have to call a method of my list fragment to tell it that something has changed.
FragmentList listfrag = (FragmentList) getFragmentManager().findFragmentByTag("fragList");
FragmentList listfrag2 = (FragmentList) getFragmentManager().findFragmentById(R.id.fraglist);
listfrag.updateData(b);
Both, listfrag and listfrag2, are always null. Why?
Edit:
based on the answer that this is not possible from one activity to another activity. I have activity A,
On a tablet A has fragment F1 and F2, on a phone only F1. If I select something on F1, the either F2 gets updated or activity B is called to show F2. In F2 I press a save button and I want to update F1 based. So I either have to call something in the same activity (tablet) or in different acitivty (phone). How can I do this without duplicating my code for the updates?
The getFragmentManager() method gets a reference to the FragmentManager that is used within that activity.
In essence, you are creating the Fragment in one Activity, but looking for it in another. You can't do that (easily).
It sounds like you might want to use startActivityForResult() and onActivityResult() to pass data back and forth between the Activities.
Fragment lifecycle is tied to its activity, you can't call it from another activity. But in the same activity when you replace with another fragment,
Try this,
trans
.replace(R.id.fragList, fragList, "fragList")
.addToBackStack(null) // this will keep fragment when you replace with another.
.commit();

FragmentManager - not able to access the fragment in another activity

I have two fragments like left panel one fragment and right side another fragment having.
The left panel fragment i having the add button from there when i click add button it launches another activity, from this activity i am trying the access the fragment but i am not getting.
This is the code i am using in my activity
LeftFragment left = (LeftFragment) getFragmentManager().findFragmentById(R.id.fragment1);
finish();
Can anyone help me.
Well, this because you can't get access to FragmentManager of other Activity, and it`s absolutely normal.
You can simply commit second fragment to the same container as a first one (use single Activity) and use method setCustomAnimations(...) for your transaction to animation.
Good luck!
you can define another ListFragment in your Fragment like this:
SecondListFragment SecondListFragment= (SecondListFragment )getFragmentManager().findFragmentById(R.id.second_list_fragment);
SecondListFragment.SetupSecondFragmentList();//its written on onListItemClick in FirstFragment
SetupSecondFragmentList() is a function that setup a list view in my Second ListFragment which has called from first one

Categories

Resources