I've been using the Android compatibility Package but i encountered the following issue, it seems that whenever i create a Fragment as an inner static class on my application and try to start that activity the it display the following error
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment org.wr.CreditCardHolderActivity.CreditCardHolderFragment: make sure class name exists, is public, and has an empty constructor that is public
And when i separate the fragment and the activity everything work smoothly, anyone know why? and how can i fix it?
Thanks!
Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy. Note: Some Android Jetpack libraries, such as Navigation, BottomNavigationView , and ViewPager2 , are designed to work with fragments.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
Yes you can, but you have to add more logic to your fragments and add some interfaces for each activity.
Understanding Fragments Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities. Fragments are standalone components that can contain views, events and logic.
If you have an inner class Fragment like:
public class SomethingFragment extends Fragment {
public static final class TypeFragment extends BaseFragment
{
public static Fragment newInstance()
{
return new TypeFragment();
}
private View mRootView;
private ListView mListView;
/**
* @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
* android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
mRootView = inflater.inflate(R.layout.fragment_category_list, container, false);
mListView = (ListView) mRootView.findViewById(R.id.fragment_listview);
return mRootView;
}
}
}
Make sure your qualifier is public when the FragmentActivity tries to reinitiate the fragment it doesn't call it from the concrete class it will handle it from the abstract FragmentActivity, and if your inner class Fragment is private the Activity has no reference to onSaveState, onRestoreState, initialise etc..
private
to public
fixed it for me!
Update:
Have a look at https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v4/java/android/support/v4/app/Fragment.java
The instantiate
method calls newInstance()
when trying to restore the Fragment
from a saved state (where the fragment was completely destroyed).
The newInstance
method requires the class to be publicly accessible, so when defined as an inner class this means it has to be public
and static
where appropriate.
Hope this clears up some future questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With