from activity back to fragment,some layout can't visiable and gone - android

in my fragment, I have a layout.when click the button,show it.and when i click it again I set the visibility to Gone.
but I set the layout visible and gone don't work when I jump to an activity and back to the fragment.then I getVisibility() == View.VISIBLE and ==View.GONE, the layout really is visible and gone. but the fragment can't show it.
private void beginTransaction(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
I doubt my fragment have some problem because when I try to use Fragment.replace to replace difference fragment, the fragment can't load some data. but I copy the replace code form my last project and the old project don't have any problem.
I use these code to solve the old problem.
bottomBar.post(new Runnable() {
#Override
public void run() {
startHomeFragment();
}
});
but I don't know how to solve the new problem now

Related

findFragmentById on back button becomes blank as size becomes zero

Here's my Flow/Structure:
Activity
Fragment
Same Fragment within it.
I have overriden removeCurrentFragment() method which does go back functionality by removing Fragment by ID as follows -
#Override
public void removeCurrentFragment() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.fragment);
if (currentFrag != null)
transaction.remove(currentFrag);
transaction.commit();
}
It seems, when this happens, as ID of both fragment is same, it creates blank view.
If more details are required, please feel free to ask in comments. I will edit question as per required details.
I thought it was related to remove fragment, But I made new fragment with separate XML, still I am having the same issue.
I have parent fragment with pagination (as I need horizontal page scroll) in it have the child fragment, when clicking on child fragment button new fragment is replaced so when it is removed the X and Y of the child fragment is also disturbed.
here is the before and after screenshot of the x and y axis of child fragment.
Before
After
can any one suggest what could be the issue?
You have to use a tag to load the Fragment, and then use that same tag again to remove it.
For example, to add the Fragment:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragmentManager.popBackStack(tag, androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE);
ft.setCustomAnimations(R.anim.enter, R.anim.exit);
ft.add(containerId, fragment, tag);
ft.addToBackStack(tag);
ft.commit();
To remove it, again use the same tag:
if (fragmentManager.findFragmentByTag(currentTag) != null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(fragmentManager.findFragmentByTag(currentTag));
ft.commit();
}

how to remove fragment after transition to next fragment

i use fragment As follows :
img_home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ft = fragmentManager.beginTransaction();
ft.replace(R.id.freamlayout,fragment_home);
ft.remove(fragment_setting);
ft.commit();
}
});
img_setting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ft = fragmentManager.beginTransaction();
ft.remove(fragment_home);
ft.replace(R.id.freamlayout,fragment_setting);
ft.addToBackStack(null);
ft.commit();
}
});
but when transition to fragment 2 , stays fragment 1
how to fix it ??
img1:
img2:
You only need to call replace() which does both:
Removes the added fragments, and adds the new one you are passing as the second argument.
See:
FragmentTransaction replace
As i can see in documentation for fragmentTransaction.replace()
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.
Check
Be sure you are using v4 fragments, because app fragments are depricated in android P.
Most easy approach
If nothing works for you, put background on parent node of both of fragment layout. so the old fragment does not appear behind this.

Android development, Closing fragment using toggle button

I think the issue is probably simple, but being pretty new to Java and to Android dev, I'm not really sure. What I'm trying to do is close a fragment that's been toggled on using a ToggleButton, but I can't find a way to then hide or close it. Any help would be appreciated.
Below is the code for the OnCheckChanged() listener.
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
MyFragment frag = new MyFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main_activity, frag, "Fragment1");
if(isChecked)
{
transaction.show(frag);
}
else
{
transaction.hide(frag);
//transaction.remove(frag);
}
transaction.commit();
}
Every time you hit onCheckedChanged, you are adding a NEW fragment and then either hiding or showing it. All other fragments you have previously created are still attached laid one on top of the other.
You should get the current fragment that is attached to the layout if there is one. If there isn't one already, create it and add it to the container.
For example, this should point you in the right direction.
private void HideFragment(){
// Get the fragment that is attached to the layout
Fragment frag=getFragmentManager().findFragmentById(R.id.centerFrame);
if (frag==null){
// The fragment does not exist yet so create one and add it
frag=new MyFragment();
getFragmentManager()
.beginTransaction()
.replace(R.id.centerFrame,frag)
.commit();
}
//Hide the fragment
getFragmentManager()
.beginTransaction()
.hide(frag)
.commit();
}
Update:
It is a little ambiguous what you are actually trying to achieve. You keep saying "close" the fragment but your code suggests you just want to toggle its visibility on and off.
If you actually want to completely remove the fragment then you should use the Remove method of the FragmentTransaction.

Replace the fragment then press back fast cause UI delayed response, shadows and corners effect are disappeared

I faced a very stranger problem. My situation is:
I built an application contain many pages, each page is a Fragment. And i used RecyclerView and CardView inside each page. My home page's layout look like:
When i click on a image item to navigate to detail page then press back button right after that, my home page is show up but:
As you can see, all shadow and corner effect disappeared, scroll didn't work, when i touch an item it take a few second before navigate detail page. When detail page shown, every things back to normal. Here is my replace fragment method:
public void replaceBackgroundFragment(Fragment mf, String tag, boolean addBackStack) {
if (mf != null && (currentFragmentTag == null || !currentFragmentTag.equals(tag))) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left,
R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.rl_background, mf, tag);
if (addBackStack) {
mf.setCanBack(true);
ft.addToBackStack(tag);
}
ft.commit();
pendingFragment = null;
pendingTag = null;
}
}
When click on an item:
public void onItemClick(MainBanner item) {
MoviePlayerFragment fragment = MoviePlayerFragment.newInstance(item.getItemID());
activity.replaceBackgroundFragment(fragment, "movie_player_fragment" + item.getItemID(), true);
}
EDIT
I used setRetainInstance(true); in my Fragment
Can anybody let me know what is happening?
you may use ft.add(R.id.rl_background, mf, tag); instead of ft.replace(R.id.rl_background, mf, tag);

addToBackStack doesn't add to fragment manager's back stack

My app has tabs and within one tab, I have a Fragment with a ListView. When an item is clicked in the list, I try to initialize a new Fragment under that tab with the following:
private class ShowItemClickListener implements OnClickListener {
public void onClick(View v) {
Fragment showDetails = new ShowFragment();
FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
ft.replace(R.id.realtabcontent, showDetails);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
System.out.println(activity.getFragmentManager().getBackStackEntryCount()); // this prints 0 though
}
}
As a result, when I press the back button in Show Fragment view, it just closes the application instead of returning to the previous fragment. What am I doing wrong here?
Sorry for late answer but maybe somebody will have same question.
I was solving the same problem. It seems actual fragment is added to backstack only if this fragment isn't in current view. So it will be added to stack after when you switch to next fragment.

Categories

Resources