How do I access a Fragments ArrayList after a screen rotation?

2

I was trying to programmatically add to my Activity a list of Fragments that implement some CardViews. These CardViews have some TextViews that I would like to define from my Activity, and in fact it works by setting them in events after the Fragment's onCreateView, such as onStart and onResume. Or rather, no ... Better to say, it almost works. When there is a screen rotation (and everything that would force the restart of an Activity), trying to do something with any Fragment, even in the onStart and onRestart methods, results in a NullPointerException. Basically I have:

MyActivity.java

private List<MyFragment> cards = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rucardapio); 

    // Check that the activity is using the layout version with
    // the fragment_container FrameLayout
    if (findViewById(R.id.fragment_container) != null) {

        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        // However, if we're being restored from a previous state,
        // then we don't need to do anything and should return or else
        // we could end up with overlapping fragments.

        if (savedInstanceState != null) {
            return;
        }

        /* Here I create 10 fragment cards */
        for(int i = 0; i < 10; i++) {
            cards.add(new MyFragment());
            transaction.add(R.id.fragment_container, cards.get(i));
        }

        transaction.commit();

   }

}

After some tests, I assumed that the problem is in if (savedInstanceState != null) { return; } . I tried these "solutions" that did not help: link , link , link and more .. .

Logcat:

01-24 09:07:21.848  30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 43
01-24 09:07:21.878  30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 46
01-24 09:07:21.878  30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.878  30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.878  30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 47
01-24 09:07:21.888  30751-30751/io.github.mths0x5f.guiaufu E/IMGSRV﹕ :0: PVRDRMOpen: TP3, ret = 48
01-24 09:07:40.088  30751-30751/io.github.mths0x5f.guiaufu E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: io.github.mths0x5f.guiaufu, PID: 30751
    java.lang.NullPointerException
            at io.github.mths0x5f.guiaufu.ru.MyActivity$2.run(MyActivity.java:161)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
            at dalvik.system.NativeStart.main(Native Method)

Code that works at application startup but not after a configuration change:

@Override
protected void onStart() {
    super.onStart();
    ((TextView) cards.get(0).getView().findViewById(R.id.textView)).setText("Test");
}
    
asked by anonymous 24.01.2015 / 19:28

2 answers

0

As no answer helped me, I say my solution: I managed to get around issues by creating a Fragment just to store data, as this "cake recipe" from the documentation itself #

With a standard conditional I check if Activity is being recreated. If it is I look for the Data Fragment and get the unmodified objects.

    
26.01.2015 / 15:38
1

Friend, understand ... If the device is rotated the activity will be destroyed and recreated. Thus, the context in which you passed your Fragment points on the activity were destroyed. What to do?

You could use setRetainInstance(true) in your Fragment. This way your Fragment will survive the recreation of the Activity:)

To resolve NPE you have to pass Context to Fragment , if the activity is recreated. Context will then belong to the new activity.

In short, without this update to every line of code that points to the activity like getActivity () or getFragmentManager (), it will lead to a NPE .

If you do not want to use the normal destruir-e-recriar process, you can use android: configChanges attributes in AndroidManifest.xml.

EXAMPLE:

<activity
    android:name=".SmsPopupActivity"
    android:theme="@android:style/Theme.Dialog"
    android:launchMode="singleTask"
    android:configChanges="orientation|keyboardHidden"
    android:taskAffinity="net.everythingandroid.smspopup.popup">
</activity>

In this way, it will not be closed when you rotate, because onCreate () não é chamado .

Sources:

link

link

Study:

link

link

    
24.01.2015 / 19:59