How to reload the currently displayed fragment - android

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.

Related

android two fragment in one activity update ui issues

I have an issue that I have an activity include in the initial one fragment shown in it ad works fine it receive keep receive data and update it's ui, but once I add the second fragment to the activity seems like the second one freeze the previous one from updating or receiving anything. have any one an idea about this issue ?
im using this function to add fragment ->
fun setFragment(fragment: Fragment, fragmentManager: FragmentManager, fragmentId: Int) {
val fragmentTransaction: FragmentTransaction =
fragmentManager.beginTransaction().setReorderingAllowed(true)
fragmentTransaction.replace(fragmentId, fragment).setTransition(TRANSIT_FRAGMENT_FADE)
fragmentTransaction.commit()
}

Call different Fragment function in Activity in Android

The code is in the Android Studio template.
Android Studio - Create new project - Navigation Drawer Activity
There are an Activity and three Fragments (HomeFragment, GalleryFragment, SlideshowFragment) in the template project. I use an AppBarConfiguration object to manage three Fragment. Now I create a function in each Fragment.
My title may be a bit vague. My problem is how to execute the method in the current fragment by clicking the button in Activity.
Now my solution is as follows.
In each fragment. Get the activity object and find the button. Then set the click method. Here is my Kotlin code.
activity?.findViewById<FloatingActionButton>(R.id.fab)?.setOnClickListener { view ->
myFunction()
}
It works fine. But I think this is not elegant enough. And there are some problems with this. If I only set the click event of one fragment, when I switch to another fragment, the click event just now will be executed after I click the button.
I guess the reason for the above problem is that the click event of the activity is bound by the fragment.
My other solution is as follows.
Create an ActivityViewModel and set a LiveData value. Change value after clicking the button. Get ActivityViewModel in fragment and observe the value in each fragment. Then execute the method when the value is changed
My third solution is as follows.
Get each fragment object and determine whether it is visible. Then perform the function of the visible fragment. Here is my Kotlin code.
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment)
var fragment1 = navHostFragment!!.childFragmentManager.fragments[0] as HomeFragment
var fragment2 = navHostFragment!!.childFragmentManager.fragments[1] as GalleryFragment
var fragment3 = navHostFragment!!.childFragmentManager.fragments[2] as SlideshowFragment
if(fragment1.isVisiable){
fragment1.myFunction()
}else if(fragment2.isVisiable){
fragment2.myFunction()
}else if(fragment3.isVisiable){
fragment3.myFunction()
}
But I still think the methods above are not elegant enough.
Can I use the interface to achieve the above functions? And how to achieve it?
I am not a native English speaker. Sorry for my bad English.
Thanks.

Custom DialogFragment - Screen Rotation Crash - Fragment XXX has not been attached yet

I am stuck with this problem from few hours now.
I have a Android App, There is Activity A. This activity has bottom navigation drawer, that uses view pager, I have 3 views, account, contact and notes.
On each view you can see list of items, as in contacts you can see list of contacts added, on notes you can see list of notes added and so on.
There is fab icon on every fragment that is basically add/edit button, when clicked it opens a dialogfragment which is used for editing or adding contacts or notes.
My problem is that I can only open my Edit dialog for Contact and notes one time. Once you rotate the screen 2-3 times, I get the error .. Fragment ContactDialogXXXX has not been attached yet.
fun editContactDialog(contact: Contact?) {
val transaction = childFragmentManager.beginTransaction()
val previous = childFragmentManager.findFragmentByTag(ContactEditDialog.name)
if (previous != null) {
transaction.remove(previous)
}
val dialog = ContactEditDialog.getInstance(contact, accountId)
dialog.show(transaction, ContactEditDialog.name)
}
I am using this above method to open dialog everytime. and only gives me error that Fragment XX has not been attached yet. after rotation. I have even done below thing from given links with no luck.
set retainInstance to true in onCreate
also added this snippet
override fun onDetach() {
super.onDetach()
callbacks = null
}
I did add
if (!isAdded)
return
but adding above line in that method only stops the crash but my dialog does not open then.
I have searched everywhere online, looked into all possible combinations on stackoverflow but my problem persists.
tried solutions from below places:
Passing an Object to Fragment or DialogFragment upon Instantiation
https://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
https://guides.codepath.com/android/using-dialogfragment#styling-dialogs
What am I missing? Is there another way of doing it? A better way of creating error prone Custom Dialog Fragments?

Android UI Problems

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.I‌​d.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!

How to find the currently displayed fragment on android using Xamarin

I'm trying to work out a way to get the currently displayed fragment on a Android app I'm working on. I have a Startup View that loads fragments, what I need to do is display a popup message when the user taps the back button but only when the using is tapping the back button from a certain fragement. For example:
Fragment A > Nav to.. > Fragment B > Nav to... > Fragment C
If the user clicks the back button from fragment C then nothing is displayed and the user goes back to fragment B as expected. But when the user click back on fragment B a popup is displayed asking the user to confirm that action before being allowed to continue back to fragment A, I hope that makes sense.
I know about the OnBackPressed() which I can override on a view but I cant touch that in a fragment. Any pointers would be appreciated.
Hi when you add a new transaction replace use a key TAG
var ft = FragmentManager.BeginTransaction();
ft.Replace(Resource.Id.details, details, "FRAGMENT1");
and if you need to check if the specify fragment is visible
Fragment myFragment = (Fragment)FragmentManager.FindFragmentByTag("FRAGMENT1");
if (myFragment.IsVisible){
//your code here
}
or if you need to get and check all of fragments inside the fragment manager you can use this
var fragmentsarray = FragmentManager.Fragments;
foreach(var fragment in fragmentsarray)
{
if (fragment.IsVisible) {
//put the code to use and get the tag to identify the current Fragment
string tag = fragment.Tag;
}
}
if need more specify Fragments info you can check the Android Docs here http://developer.android.com/reference/android/app/Fragment.html
Hope it helps you

Categories

Resources