0

I am working on a problem to fix Activity restart on screen rotation, I am implementing the 2nd part of this answer, it gives me the error in setContentView(R.layout.fragment_introduction).

The method setContentView(int) is undefined for the type IntroductionFragment.

public class IntroductionFragment extends Fragment {

public IntroductionFragment(){}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_introduction,container,false);

    return rootView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.fragment_introduction);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.indianconstitution"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<application
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.indianconstitution.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

How to fix this error?

Thanks in advance

Community
  • 1
  • 1
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77

2 Answers2

1

setContentView is a method of Activity. You are inside a Fragment. The View hierarchy for the fragment is build upon the return value of onCreateView

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Your onConfigurationChanged() method should look like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
    LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.fragment_introduction, null);
    ViewGroup rootView = (ViewGroup) getView();
    rootView.removeAllViews();
    rootView.addView(view);

}
Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120