I have a problem starting an activity to pick an image from the gallery when I start it from the 3rd level of FragmentActivity/Fragment combinations. If I start it from the 1st or 2nd level, it works. In the app, there are up to 5 levels of FramentActivity/Fragment combinations running without a problem, but I don't start the activity to pick an image from them.
Is there a maximum level of FramentActivity/Fragment combinations that I can start? Or what am I doing wrong?
The FragmentActivity starts the Fragment like this:
public class MyActivity extends FragmentActivity
{
private MyFragment _fragment;
...
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.simple_fragment_container_activity );
// create the fragment to show
FragmentManager fm = getSupportFragmentManager();
_fragment = (MyFragment)fm.findFragmentById( R.id.fragment_container );
// If the Fragment is non-null, then it is currently being retained across a configuration change.
if( _fragment == null )
{
_fragment = new MyFragment();
// start the new fragment
FragmentTransaction ft = fm.beginTransaction();
ft.add( R.id.fragment_container, _fragment );
ft.commit();
}
}
The Fragment code is:
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
// retain this fragment across configuration changes
setRetainInstance( true );
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View v = inflater.inflate( R.layout.my_fragment, container, false );
...
return v;
}
In the OnClickListener of a button, I start the image picker like this:
Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
intent.setType( "image/*" );
startActivityForResult( intent, 20 );
As soon as I close the image picker (no matter if selecting an image or not), my app crashes before resuming it. The onActivityResult method is sometimes called fully, sometimes partly. It does nothing than log the image path for the moment:
if( resultCode != Activity.RESULT_OK ) return;
if( requestCode == 20 )
{
Uri selectedImage = intent.getData();
Log.w( null, "image picked: " + selectedImage.toString() );
}
The crash writes this to the LogCat and does not happen in my code IMO:
05-19 08:18:19.210: D/AndroidRuntime(24592): Shutting down VM
05-19 08:18:19.210: W/dalvikvm(24592): threadid=1: thread exiting with uncaught exception (group=0x41826da0)
05-19 08:18:19.220: E/AndroidRuntime(24592): FATAL EXCEPTION: main
05-19 08:18:19.220: E/AndroidRuntime(24592): Process: ch.infero.testapp, PID: 24592
05-19 08:18:19.220: E/AndroidRuntime(24592): java.lang.RuntimeException: Unable to resume activity {ch.infero.testapp/ch.infero.testapp.settings.MyActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2946)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2975)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.os.Looper.loop(Looper.java:157)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-19 08:18:19.220: E/AndroidRuntime(24592): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 08:18:19.220: E/AndroidRuntime(24592): at java.lang.reflect.Method.invoke(Method.java:515)
05-19 08:18:19.220: E/AndroidRuntime(24592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-19 08:18:19.220: E/AndroidRuntime(24592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-19 08:18:19.220: E/AndroidRuntime(24592): at dalvik.system.NativeStart.main(Native Method)
05-19 08:18:19.220: E/AndroidRuntime(24592): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:133)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.database.CursorWrapper.requery(CursorWrapper.java:186)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.Activity.performRestart(Activity.java:5471)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.Activity.performResume(Activity.java:5497)
05-19 08:18:19.220: E/AndroidRuntime(24592): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2936)
05-19 08:18:19.220: E/AndroidRuntime(24592): ... 10 more