Fatal Exception: java.lang.IllegalStateException Fragment Not attached to an Activity - android

Hello Im facing an issue of "fragment not attached to an activity" on activityviewmodels()
my code:
private val dataViewModel: DataViewModel by activityViewModels()
I have written this code in Fragment which is attached to ViewPager.
and Viewpager is Attached to BaseFragment.
I have tried adding null check but still facing issue.

You can try using setOffScreenPageLimit(). (If this is the use-case)

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()
}

Binding becomes when fragment is visible in ViewPager

Problem:
I have a ContainerFragment in which there is a ViewPager. CameraFragment is being shown in this viewpager. The shutter button is in ContainerFragment and I'm using an interface to receive callback in CameraFragment when shutter button is clicked in order to click picture. When callback is received then I try to capture image and app crashes with error.
Log:
java.lang.IllegalStateException: should never call auto-cleared-value get when it might not be available
at com.example.chat.utils.services.AutoClearedValue.getValue(AutoClearedValue.kt:51)
at com.example.chat.ui.base.BaseFragment.getBinding(BaseFragment.kt:41)
at com.example.chat.ui.camera.camera.CameraFragment.captureImage(CameraFragment.kt:127)
at com.example.chat.ui.camera.camera.CameraFragment.onShutterClicked(CameraFragment.kt:135)
at com.example.chat.ui.camera.camerafragmentcontainer.CameraContainerFragment.handleShutterClick(CameraContainerFragment.kt:94)
Solved this issue. The problem was that when I was initializing the interface I was creating an instance of CameraFragment() and again in viewpager a new instance was being created. so just passed the same instance in viewpager and the problem solved.

Espresso: Not able to launch inner fragment

I am using launchFragmentInContainer<FragmentOne>() to launch fragment and able to perform actions using EspressoMatchers, it's all working fine.
But the issue comes when I want to launch another fragment onClick Of button(this code is inside FragmentOne) but while launching getting an error for FragmentTwo
Example:
onClick {
val fragment = FragmentTwo.newInstance()
val transaction = requireActivity().supportFragmentManager.beginTransaction()
transaction.add(fragmentContainerID, fragment)
transaction.commit()
}
Above code giving error java.lang.IllegalArgumentException: No view found for id 0x4d2 (unknown) for fragment FragmentTwo{bc39d06} (6f4636b5-f441-4e38-956a-47ac91d261bc) id=0x4d2}
How to resolve this inside fragment? or I have to do something in espresso test.
This issue is coming due to fragmentContainerID which should be unique and used under the hood of launchFragmentInContainer().
So, inside the espresso framework android.R.id.content is used as a fragment Id. This same id should be used while the fragment transaction.
Code Snippet:
var fragmentContainerID = android.R.id.content // It will do the trick and fragment will be added on top of stack

Fatal Exception: java.lang.IllegalStateException: Failure saving state: active fragment was removed from the FragmentManager

I recently changed my app to target API Level 28 and also started using androidx instead of support libraries. After the change, I'm noticing a crash that has the following traceback
Fatal Exception: java.lang.IllegalStateException: Failure saving state: active MyFragment{22caf6fc (04a7bbf5-8806-4a45-a25d-616ed244bf18) id=0x7f1000ff} was removed from the FragmentManager
at androidx.fragment.app.FragmentManagerImpl.saveAllState(FragmentManagerImpl.java:2301)
at androidx.fragment.app.FragmentController.saveAllState(FragmentController.java:150)
at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:496)
at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:510)
at com.company.utils.MyAppCompatActivity.onSaveInstanceState(MyAppCompatActivity.java:161)
at android.app.Activity.performSaveInstanceState(Activity.java:1311)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1288)
at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4166)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3577)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3633)
at android.app.ActivityThread.access$1300(ActivityThread.java:164)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1491)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:5541)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
All my activities are extending from a base activity MyAppCompatActivity which extends from AppCompatActivity. And in the onSaveInstanceState method of MyAppCompatActivity, I have a few log lines there. I have three fragments in this activity implemented under a ViewPager with FragmentStatePageAdapter and the activity itself is not overriding onSaveInstanceState method.
I went through a few SO posts close to this such as this one and this one, but it looks as if it's happening when we're using FragmentManager's methods to manage the fragments ourselves rather than using a ViewPagerAdapter handle it. I'm not doing that and the closest thing that I'm doing away from the usual flow is overriding FragmentStatePageAdapter's methods instantiateItem and destroyItem to help me have a method to get a fragment's reference in the code as given in this SO answer.
Which Fragment library version did you use? There is an issue with 1.1.0-alpha01.
I am using the latest gradle as below
implementation 'androidx.core:core:1.1.0-alpha03'
I have compared crashing fragment with other fragment code and found the difference as below:
Crashing fragment has method setRetainInstance(true); inside onCreateView() as shown in below snippet.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setRetainInstance(true);
}
I come across the link which says not to use setRetainInstance : setRetainInstance - This can only be used with fragments not in the back stack
I removed the setRetainInstance(true) for crashing fragmnet; It's working now.
How to replicate this issue:
MainActivity --> HomeFragment --> FirstFragment(crashing fragment) --> HomeFragment
MainActivity loads with HomeFragment which replaced by FirstFragment and then going back to HomeFragment.
Then moving my app to background and crash happens with below exception.
java.lang.IllegalStateException: Failure saving state: active
FirstFragment{fd50037 (4a8b618e-a0a8-45d0-aa37-ba08393b8f68)
id=0x7f08009c} was removed from the FragmentManager
Add a dependency in build.gradle app
implementation 'androidx.preference:preference:1.1.0-alpha05'
it will works for me, thanks to
Refer this - https://issuetracker.google.com/issues/119256498
my issues are in run time I'm giving permission to camera and storage after that seconds the camera is open my app is crashed. I use this my issue is solved
I didn't have any setRetainInstance(true);
When i was going through my code i saw there was another transaction which i was doing using fragment manager in onResume for the same fragment .
This issue is sometime caused by a double click on a button which finishes the current activity.
add :
button.setEnable="false";
for all buttons in your view ;)
enjoy

MvvmCross ViewPager not show in Activity

I want to show a few tabs sharing the same viewmodel with viewpager in an activity. I am using MvvmCross 5.6.2.
When I do that in a fragment, the viewpager is shown properly. Using the same method, I add tab fragments to the viewpager in an activity with code like this:
var viewPagerFragmentList = new List<MvxViewPagerFragmentInfo>
{
new MvxViewPagerFragmentInfo("Personal", typeof (SettingPersonalFragment), ViewModel),
new MvxViewPagerFragmentInfo("Preference", typeof (SettingPreferenceFragment), ViewModel)
};
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(this, SupportFragmentManager, viewPagerFragmentList);
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs_fragment_setting_viewpager);
tabLayout.SetupWithViewPager(viewPager);
The tab fragments are not shown on screen, though the tab fragment's OnCreate and OnCreateView are executed.
I found the latest sample Playground is showing tabs in activity too. Following the sample, I register the fragment:
[MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs_fragment_setting_viewpager, ViewPagerResourceId = Resource.Id.viewpager_fragment_setting_viewpager, Title = "Personal", ActivityHostViewModelType = typeof(SettingViewModel))]
No luck with that. The last thing I found in Playground to show a tab is to navigate to the fragment viewmodel:
_navigationService.Navigate<Tab2ViewModel>();
_navigationService.Navigate<Tab3ViewModel>();
However, in my case, I do not have a viewmodel for each fragment. The workaround on my hand is to move the viewpager to a fragment in the activity.
How could I show the viewpager fragments without their own viewmodels in an activity?
Thanks,
Nick
The above code that works in fragment also work in activity. There is an issue in the xml causing the viewpager not showing. Sorry.
Thanks everyone who helped.

Categories

Resources