Search code examples
android-fragmentactivity

keeping data during orientation changes by using fragments


I am trying to understand fragments. All the examples I read is about having a main activity and then two sub fragments. The issue is that my program has only 1 activity. Can I make it a fragment? Current ui main class

public class MainActivity extends Activity {}

Can I make it

public class MainActivity extends Fragment {}

The program I am trying to create will constantly monitor phone variables and display logs in a textView. I don't want to lose the logs when the user changes the orientation or some other change when the os destroys the ui.

The UI is basically 1 textView that gets filled by a runnable that updates the textView every 5 seconds with the content of a linked list. I don't want to lose the content of the linked list basically.

I checked getLastNonConfigurationInstance() but it seems it's depreciated so I don't want to use that

Any help would be highly appreciated.

Amish


Solution

  • Found answer here: http://blog.webagesolutions.com/archives/458

    So after a lot of searching I found that if you add the following: android:configChanges="orientation|screenSize"

    in the androidManifest.xml, the os will not destroy the activity when switching from portrait mode to landscape mode.

    My xml file looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.axr0284.phonecontrol"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="15" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.axr0284.phonecontrol.MainActivity"
                android:label="@string/app_name" 
                android:configChanges="orientation|screenSize">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    Hope this helps somebody, Amish