Previous fragment edittext focus issue - android

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

Related

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

how to restore last opened fragment inside an activity after getting back from another actvivty

I'm using multiple fragments inside an activity like this flow: Fragment A has a list on it's item click Fragment B opens and it also has a list opens Fragment C which has a list Open another Activity , The problem is when I go back from the other Activity I found Fragment A is opened, How I restore the last Fragment C when go back from the other activity?
here is the code of replacing Fragment inside my activity
protected void showFragment(Fragment fragment) {
String TAG = fragment.getClass().getSimpleName();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_home, fragment, TAG);
fragmentTransaction.addToBackStack(TAG);
fragmentTransaction.commit();
}
replace removes the existing fragment and adds a new fragment. This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked. Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active.
Use add instead of replace
fragmentTransaction.add(R.id.container_home, fragment, TAG);
I had the same issue solved using the above code.
You can use
fragmentTransaction.addToBackStack(null);
instead of,
fragmentTransaction.addToBackStack(TAG);
Hope Your problem will solve.

Should I replace fragment or modify current one?

Fragments can be added/replaced/removed dynamically in the code. I have been using this method for my projects.
However I wonder what would be the difference (mainly in performance) if instead of creating a new fragment and adding it or replacing the current one, we just grab a handle to the current one and modify it.
For example, consider this simple scenario where the only purpose of a fragment is to display an image, and we want to modify that image:
Option 1) Create a new instance of my Fragment class and replace the fragment which is currently displayed:
MyFragmentClass fr = new MyFragmentClass();
fr.setImage(1);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.frame_layout_id, f)
...
Option 2) Just grab a reference to the currently displayed fragment and modify it:
MyFragmentClass fr = (MyFragmentClass)
fragmentManager.findFragmentById(R.id.frame_layout_id);
fr.setImage(1);
Is the whole process of creating a new fragment instance and adding it more efficient than calling findFragmentById?
Go through following information may be you get clarification
1- fragmentTransaction.addToBackStack(str);
Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.
2- fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag)
It replace an existing fragment that was added to a container.
This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
3- fragmentTransaction.add(int containerViewId, Fragment fragment, String tag)
Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.
What does it mean to replace an already existing fragment, and adding a fragment to the activity state and adding an activity to the back stack ?
There is a stack in which all the activities in the running state are kept. Fragments belong to the activity. So you can add them to embed them in a activity.
When you navigate to the current layout, you have the id of that container to replace it with the fragment you want.
You can also go back to the previous fragment in the backStack with the popBackStack() method. For that you need to add that fragment in the stack using addToBackStack() and then commit() to reflect. This is in reverse order with the current on top.
findFragmentByTag does this search for tag added by the add/replace method or the addToBackStack method ?
If depends upon how you added the tag. It then just finds a fragment by its tag that you defined before either when inflated from XML or as supplied when added in a transaction.

Android: How to start a new fragment without destroying the state of the previous fragment?

How to replace Fragment_1 Fragment_2 with the ability to return back to the state and Fragment_1 (scroll, inputs, forms, other)?
enter image description here
If you use Activities, then you can use the method startActivityForResult. The work of this method perfectly shows the current task with fragments.
I do not want to save the values and then substitute them. We need that to Fragment_1 remained intact.
How to save the status of previous fragments at the opening of the new fragment_N and when you turn the screen using backstack?
Is there a ready library for the implementation of these tasks?
Instead of replacing fragment, You can add a new fragment and hide current one.
Something like this
FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(your_current_fragment);
t.add(container, new_fragment);
t.addToBackStack(TAG);
t.commit();
It will not loss the state of hidden fragment
FragmentTransaction t = getFragmentManager.beginTransaction();
t.hide(your_current_fragment);
t.add(container, new_fragment);
t.addToBackStack(TAG);
t.setCustomAnimations(R.anim.open_enter,R.anim.close_exit,R.anim.open_enter,R.anim.close_exit)
or
t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
t.setTrans
t.commit();
replace FragmentPagerAdapter with a HorizontalScrollView contains 2FrameLayout for fragment.You can freely change fragment in FrameLayout.

"Can't change container ID of fragment" when using "replace"

So I've found a few similar cases, but nothing about this specific case.
I have a FrameLayout which I gave the id "container", and contains a few different fragments.
In my code for the activity that contains that FrameLayout, I'm trying to switch between the fragments with a function that receives a fragment.
In that code:
a. I have defined private FragmentManager fm = getSupportFragmentManager();
b. I have defined FragmentTransaction ft; to use later.
c. My function is:
private void setActiveFragment(Fragment fragment){
//Determine which button should be marked as "active"
determineButtonByFragment(fragment);
//Repalce fragment
ft = fm.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack(String.valueOf(fragment.getId()));
ft.commit();
}
Any idea why i would get this error on the "replace"?
EDIT:
Ok, I just realized a bit more about fragments. Since all 5 fragments are added to the FrameLayout, the "replace" won't work. You get the "can't chance container ID error" when you're trying to move a fragment from 1 parent view to another (Or in this, case, to the same one), without detaching it first.
Let's say I have fragments A, B, C, D, E.
If I want to replace to fragment B right now, I won't be able to do it until I remove it from it's original parent (Or at least that's how I think it works. please enlighten me otherwise). The only question that remains now, is how do I switch between my fragments correctly...
Alright, apparently you cannot use the "replace" method on fragments which are hard-coded in the layout:
Replacing a fragment with another fragment inside activity group
What I have to do now, is inside the function, to find a way to determine the fragment I want to display, create a new instance of it, and use "replace" on it.

Categories

Resources