Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Unable to instantiate fragment" when restore app from background after a long time #238

Open
Mohammad-Adam opened this issue Jan 15, 2021 · 2 comments

Comments

@Mohammad-Adam
Copy link

Mohammad-Adam commented Jan 15, 2021

Stack trace:

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:206)
at android.app.ActivityThread.main(ActivityThread.java:6784)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:108)
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.********.ui.PrivateOnlinePostsFragments: could not find Fragment constructor
at androidx.fragment.app.Fragment.instantiate(Fragment.java:563)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:390)
at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:74)
at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2466)
at androidx.fragment.app.FragmentController.restoreSaveState(FragmentController.java:196)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:287)
at androidx.appcompat.app.AppCompatActivity.onCreate(AppCompatActivity.java:115)
at com.********.ui.MainActivity.onCreate(MainActivity.kt:63)
at android.app.Activity.performCreate(Activity.java:6984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857)
... 10 more
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getConstructor(Class.java:1725)
at androidx.fragment.app.Fragment.instantiate(Fragment.java:548)
... 21 more

frag-nav:3.3.0

@Mohammad-Adam
Copy link
Author

using super.onCreate(null) in MainActivity's onCreate as a workaround for now

@Mohammad-Adam Mohammad-Adam changed the title help asap plz: "Unable to instantiate fragment" when restore app from background after a long time "Unable to instantiate fragment" when restore app from background after a long time Jan 27, 2021
@Antarix
Copy link

Antarix commented Sep 26, 2022

This happen when we use parameterized constructor for fragment. If you want to pass parameter in fragment, then try passing it in bundle e.g in your case

public static final PrivateOnlinePostsFragments newInstance(int title, String message){
    PrivateOnlinePostsFragments fragment = new PrivateOnlinePostsFragments();
    Bundle bundle = new Bundle(2);
    bundle.putInt(EXTRA_TITLE, title);
    bundle.putString(EXTRA_MESSAGE, message);
    fragment.setArguments(bundle);
    return fragment ;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants