0

I wanted to add the setRetainInstance(true) method to my Application in the onCreate method, but it's not working. Android Studio tells me it can't resolve the method setRetainInstance.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRetainInstance(true);

    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

Error:(47, 9) error: cannot find symbol method setRetainInstance(boolean)

Zazama
  • 275
  • 2
  • 10
  • `setRetainInstance()` is a method in `Fragment` and your code seems to be in an `Activity`. What do you want to achieve? – laalto Apr 08 '15 at 19:16

2 Answers2

1

This is a Fragment class method, not from Activity, see here:

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

[edit]

answer to your additional information:

'll try to explain it: I created a standard navigation drawer activity in Android studio and started programming. If I change the orientation of the app, the app will crash because it doesn't rebuild the Elements that I've setup in the activity_main.xml.

there are many ways for handling this problem:

  1. Serialize your object, ie. making it Parcelable: How can I make my custom objects Parcelable?. Then persist/restore it in Activity.onSaveInstanceState/onCreate.
  2. Put your object into retained fragment instance: example here: Retaining an Object During a Configuration Change
  3. Keep your object between Activity config changes using onRetainNonConfigurationInstance (which is now deprecated).
  4. You might find a quick solution by adding android:configChanges to your activity manifest, but thats bad design, for more info look here: Why not use always android:configChanges="keyboardHidden|orientation"?

actually the best aproach is to start with ability to rebuild your object on config changes. Android can destory your app (when its in background), killing your activity object, then when user will want to move it to foreground you will have to recreate your object anyway.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
0

As said by @marcinj, this is a Fragment method. Try explaining what you want to achieve so we can give you a more complete answer.

If you already have a fragment, you need to invoke the setRetainInstance method from there. If you don't have one, but still need to change the retainInstance attribute for some reason, create one: http://developer.android.com/training/basics/fragments/creating.html

Danilo Prado
  • 2,260
  • 1
  • 13
  • 16
  • I'll try to explain it: I created a standard navigation drawer activity in Android studio and started programming. If I change the orientation of the app, the app will crash because it doesn't rebuild the Elements that I've setup in the activity_main.xml. The error message from logcat is the following: "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.FrameLayout.addView(android.view.View)' on a null object reference" – Zazama Apr 09 '15 at 13:45
  • @Zazama there are lots of topics on this problem which you can google, I have summarized most common aproaches to it in my answer. – marcinj Apr 10 '15 at 07:51