Reputation: 455
I faithfully followed the guidelines here: http://developer.android.com/guide/topics/resources/runtime-changes.html. I was surprised to find how easy it was to handle configuration changes using RetainedFragment. Everything works properly in normal flow of events. But there is a glitch. If I minimize the app and start it after a long time it crashes with the following:
E/AndroidRuntime( 5734): FATAL EXCEPTION: main^M E/AndroidRuntime( 5734): Process: net.citibuzz.app.scool, PID: 5734^M
E/AndroidRuntime( 5734): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.citibuzz.app.scool/net.citibuzz.app.scool.MapActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment net.citibuzz.app.scool.RetainedFragment: make sure class name exists, is public, and has an empty constructor that is public^M
E/AndroidRuntime( 5734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)^M
E/AndroidRuntime( 5734): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)^M
E/AndroidRuntime( 5734): at android.app.ActivityThread.access$800(ActivityThread.java:139)^M
E/AndroidRuntime( 5734): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)^M
E/AndroidRuntime( 5734): at android.os.Handler.dispatchMessage(Handler.java:102)^M
E/AndroidRuntime( 5734): at android.os.Looper.loop(Looper.java:136)^M
E/AndroidRuntime( 5734): at android.app.ActivityThread.main(ActivityThread.java:5102)^M
E/AndroidRuntime( 5734): at java.lang.reflect.Method.invokeNative(Native Method)^M
E/AndroidRuntime( 5734): at java.lang.reflect.Method.invoke(Method.java:515)^M
E/AndroidRuntime( 5734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)^M
E/AndroidRuntime( 5734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)^M
E/AndroidRuntime( 5734): at dalvik.system.NativeStart.main(Native Method)^M
E/AndroidRuntime( 5734): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment net.citibuzz.app.scool.RetainedFragment: make sure class name exists, is public, and has an empty constructor that is public^M
E/AndroidRuntime( 5734): at android.support.v4.app.Fragment.instantiate(Fragment.java:413)^M
E/AndroidRuntime( 5734): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)^M
E/AndroidRuntime( 5734): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)^M
E/AndroidRuntime( 5734): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)^M
E/AndroidRuntime( 5734): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:97)^M
E/AndroidRuntime( 5734): at net.citibuzz.app.scool.MapActivity.onCreate(MapActivity.java:134)^M
E/AndroidRuntime( 5734): at android.app.Activity.performCreate(Activity.java:5248)^M
E/AndroidRuntime( 5734): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)^M
E/AndroidRuntime( 5734): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)^M
E/AndroidRuntime( 5734): ... 11 more^M
E/AndroidRuntime( 5734): Caused by: java.lang.InstantiationException: can't instantiate class net.citibuzz.app.scool.RetainedFragment; no empty constructor^M
E/AndroidRuntime( 5734): at java.lang.Class.newInstanceImpl(Native Method)^M
E/AndroidRuntime( 5734): at java.lang.Class.newInstance(Class.java:1208)^M
E/AndroidRuntime( 5734): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)^M
E/AndroidRuntime( 5734): ... 19 more^M
Apparently the system ran out of resources, killed the activity instance and then tried to recreate it on restart. This is a bit confusing for me. Looks like the system is handing two cases differently : one where it destroys activity due to configuration change and another when it destroys it due to resource crunch. I reckon this must be a common problem. How do I handle this ?
I am happy to provide code snippets if required , but as I said at the beginning I have followed Google's sample code to the T.
Edit: OK, I realise I did make some changes of my own. I added a constructor :-(
public class RetainedFragment extends Fragment {
// data object we want to retain
private MapState data;
private Context mContext;
public RetainedFragment(Context context) {
mContext = context;
}
// this method is only called once for this fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain this fragment
setRetainInstance(true);
}
public void setData(MapState data) {
this.data = data;
}
public MapState getData() {
return data;
}
@Override
public void onDestroy() {
super.onDestroy();
}
...
}
Upvotes: 0
Views: 504
Reputation: 26198
Caused by: java.lang.InstantiationException: can't instantiate class net.citibuzz.app.scool.RetainedFragment;
You cant have a custom constructor within the fragment that it need to call its empty constructor, if it is not define then that exception will appear
here is the documentation about it:
All subclasses of Fragment must include a public empty constructor.
The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the empty constructor is not available, a runtime exception will occur in some cases during state restore.
By the way you can call the reference to context from the fragment by calling getActivity();
to call the activity's context
If you want to instantiate your fragment then you need to create a static method that will return the reference of the newly created fragment
public static RetainedFragment newInstance(int index) {
RetainedFragment f = new RetainedFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
Upvotes: 1