I have a strange behaviour with the Android UI. At the beginning all works fine but after a minute the fragment always gets blank when replacing it with the FragmentManager. When reopening the app all looks fine again. I am using an AppCompatActivity does someone know a solution for this?
This is my replace code
var fragmentTransaction = FragmentManager.BeginTransaction();
fragmentTransaction.Replace(Resource.Id.frameLayout1, nextFrag).AddToBackStack("div_overview").Commit();
Edit:
I open the first fragment by using the following code in the MainActivity :
SupportFragmentManager.BeginTransaction().Replace(Resource.Id.frameLayout1, fragments[1]).Commit();
This Fragment can open other fragments by button click. For this i am using
var fragmentTransaction = FragmentManager.BeginTransaction(); fragmentTransaction.Replace(Resource.Id.frameLayout1, nextFrag).AddToBackStack("div_overview").Commit();
This behaviour only happens after some time (mostly after the screen has been darken). The first transactions are working without any problems...
Thank you in advance!
Related
I'm handling Login and Logout Logic in a Fragment. I want the fragment to refresh itself when the user logs in or logs out. I'm using Navigation Component for my Fragment Navigation.
I've tried several ways but none of them are working. I'm trying the following code:
val fragManager = this#WelcomeFragment.parentFragmentManager
fragManager.beginTransaction().detach(this).attach(this).commit()
However, this code does nothing.
I've also tried doing the following:
val act = this.requireActivity() as MainActivity
val fragmentTransaction =
act.supportFragmentManager.beginTransaction()
fragmentTransaction.setReorderingAllowed(true)
fragmentTransaction.detach(this).attach(this)
Though this crashes my App. How can I refresh/reload a currently displayed fragment? Thanks.
EDIT: I want to solve problem of duplicating fragment in view Pager, removing them isn't the best solution.
I have a problem with FragmentManager in my app. Every time when I click "Wykłady teoretyczne" or "Wykłady popularnonaukowe" on Navigation Drawer my app is creating new fragment ScheduleFragment() which contains ViewPager and in SchedulePagerAdapter app is creating new 3 fragments (ScheduleEventFragment()) to swipe them in ScheduleFragment. My problem is that FragmentManager doesn't remove old ScheduleEventFragments, for all the time it keeps them in memory. How can I fix this problem?
I have my project on GitHub, please check it.
There you have screen from debuging
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.remove(R.id.container,fragment,optionalTag);
fragmentTransaction.commit();You can also detach or replace
Always check if fragmemt.isAdded() and then only add the fragment if not.
I'm working on a application which has Single activity rest all fragments...
This issue i'm facing occurs occasionally.. like when ever i press back button from any of the fragment the previous fragment appears... but sometimes it happens that My activity shows blank screen with action bar present, and rest of the screen is blank instead of showing the fragment... Since this issue appears occasionally i'm not able to debug it also... Any idea why it happens so??
Here is the code fragment transaction..
private void loadFragment(Fragment fragment, int activityNumber) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction trnx = manager.beginTransaction();
trnx.replace(R.id.fragPage, fragment, "Current_Fragment");
if (activityNumber != FragmentActivityNumbers.HOME_ORDERNUM) {
trnx.addToBackStack(null);
}
trnx.commit();
}
I suggest to test if its the first fragment so you use Add instead of replace then you can use replace for the others fragments .
I've been working for several months in an app. The version uploaded to the playstore works fine. Recently, doing some improvements, the whole app has started to work in a very strange way.
If you swith between fragments, after a while it does not inflate properly the fragments, putting always the content of the first one. So, if you switch the fragment, the action bar changes but the content is always the same (and you cannot interact with it).
Here is a quick video of the error https://streamable.com/9exa
I am going crazy as there is no log or trace whatsoever.
I am using the usual way of swithing fragments and the most intriguing part is that at the beginning everything works ok:
Fragment fragment = getSelectedFragment(index); // returns the desired fragment
FragmentManager fragmentManager = getFragmentManager();
//Pop backstack
for(int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
Do anyone has any kind of idea what could be happening here? Thanks!
edit: I also have tried to clear the backstack in case it was the problem, but it doesn't change anything
solved: As I explain in my answer, I have discovered that is a problem related to a SwipeRefreshLayout. More information about this problem can be found at When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
Ok, I found out the problem.
It seems that a SwipeRefreshLayout was the one causing all the problems. One of my fragments was using it, and after switching fragments a couple of times it gets stuck.
Removing it, or disabling it setEnabled(false) solves all the problems.
I hope it will help to anyone that is having the same problem!
Another thread in SO that refers to the same problem: When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
What fragments are you using?
I had some strange behaviour using framework fragments, and i switched to V4 and everything worked fine
Please check this link: FragmentManager popBackStack doesn't remove fragment
I am using the following code in my activity for switching between fragments and it is working fine. My app's minSdkVersion is 15.
private void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
}
I have a tabbed application, say 2 tabs. It was initially developed using ActivityGroup. However, having realized that multi-panes are not supported in ActivityGroup, I decided to use fragments. Before going to the problem, let me brief you about the application.
So, like I said before it is a tabbed application
TabA---> Activity1---> Activity2---> Activity3
TabB---> Activity4---> Activity5----> Activity6
This is the work pattern of my old activity-group based app.
Now with fragments, this would change into something like below
FragmentActivity---> TabAFragment---> FragmentA1---> FragmentA2
|
---> TabBFragment---> FragmentB1---> FragmentB2
Each fragment connects to server for data on initial load.
And in the transaction, I replace fragment every time I add.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fragment);
ft.commit();
What is observed
I have only one activity(FragmentActivity-where tabs are created) for those two tab fragments. I share this activity for all tabs. For instance, I load fragmentB1 in tabB- inflates a view, fetches data from server and displays in a ListView. Then I switch to other tabA and loads fragmentA1. So far so good. Now if I go back to tabB, I want to see the listView which was loaded earlier. What happens is it starts everything from square one.
This is my first-hand experience with fragments. I did a bit of research; however it didn't really help fix mine.
How can I retain already loaded view?
Any thoughts?
Hi I'm fairly new to all the fragments also. However, I was doing something similar and found that I needed to add the current fragment to the back stack like so.
ft.addToBackStack (null);
This line would go just before your comit so your code becomes:
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.realtabcontent, fragment);
ft.addToBackStack (null);
ft.commit();
Hopefully this is of some use.