Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

sample of onSaveInstanceState() at fragment level

  • Loading branch information...
commit bc021f4bff0dea2a86fa0d03bb839ad41394353f 1 parent ffafecd
Mark Murphy authored March 19, 2013
8  ConfigChange/FragmentBundle/.classpath
... ...
@@ -0,0 +1,8 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<classpath>
  3
+	<classpathentry kind="src" path="src"/>
  4
+	<classpathentry kind="src" path="gen"/>
  5
+	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
  6
+	<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
  7
+	<classpathentry kind="output" path="bin/classes"/>
  8
+</classpath>
33  ConfigChange/FragmentBundle/.project
... ...
@@ -0,0 +1,33 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<projectDescription>
  3
+	<name>RotationFragmentBundleDemo</name>
  4
+	<comment></comment>
  5
+	<projects>
  6
+	</projects>
  7
+	<buildSpec>
  8
+		<buildCommand>
  9
+			<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
  10
+			<arguments>
  11
+			</arguments>
  12
+		</buildCommand>
  13
+		<buildCommand>
  14
+			<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
  15
+			<arguments>
  16
+			</arguments>
  17
+		</buildCommand>
  18
+		<buildCommand>
  19
+			<name>org.eclipse.jdt.core.javabuilder</name>
  20
+			<arguments>
  21
+			</arguments>
  22
+		</buildCommand>
  23
+		<buildCommand>
  24
+			<name>com.android.ide.eclipse.adt.ApkBuilder</name>
  25
+			<arguments>
  26
+			</arguments>
  27
+		</buildCommand>
  28
+	</buildSpec>
  29
+	<natures>
  30
+		<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
  31
+		<nature>org.eclipse.jdt.core.javanature</nature>
  32
+	</natures>
  33
+</projectDescription>
32  ConfigChange/FragmentBundle/AndroidManifest.xml
... ...
@@ -0,0 +1,32 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3
+	package="com.commonsware.android.rotation.fragbundle"
  4
+	android:versionCode="1"
  5
+	android:versionName="1.0.0">
  6
+
  7
+	<uses-sdk
  8
+		android:minSdkVersion="5"
  9
+		android:targetSdkVersion="14"/>
  10
+
  11
+	<supports-screens
  12
+		android:anyDensity="true"
  13
+		android:largeScreens="true"
  14
+		android:normalScreens="true"
  15
+		android:smallScreens="true"/>
  16
+
  17
+	<application
  18
+		android:icon="@drawable/ic_launcher"
  19
+		android:label="@string/app_name"
  20
+		android:theme="@style/Theme.Sherlock">
  21
+		<activity
  22
+			android:name=".RotationFragmentDemo"
  23
+			android:label="@string/app_name">
  24
+			<intent-filter>
  25
+				<action android:name="android.intent.action.MAIN"/>
  26
+
  27
+				<category android:name="android.intent.category.LAUNCHER"/>
  28
+			</intent-filter>
  29
+		</activity>
  30
+	</application>
  31
+
  32
+</manifest>
85  ConfigChange/FragmentBundle/build.xml
... ...
@@ -0,0 +1,85 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<project name="RotationOneDemo" default="help">
  3
+
  4
+    <!-- The local.properties file is created and updated by the 'android' tool.
  5
+         It contains the path to the SDK. It should *NOT* be checked into
  6
+         Version Control Systems. -->
  7
+    <loadproperties srcFile="local.properties" />
  8
+
  9
+    <!-- The ant.properties file can be created by you. It is only edited by the
  10
+         'android' tool to add properties to it.
  11
+         This is the place to change some Ant specific build properties.
  12
+         Here are some properties you may want to change/update:
  13
+
  14
+         source.dir
  15
+             The name of the source directory. Default is 'src'.
  16
+         out.dir
  17
+             The name of the output directory. Default is 'bin'.
  18
+
  19
+         For other overridable properties, look at the beginning of the rules
  20
+         files in the SDK, at tools/ant/build.xml
  21
+
  22
+         Properties related to the SDK location or the project target should
  23
+         be updated using the 'android' tool with the 'update' action.
  24
+
  25
+         This file is an integral part of the build system for your
  26
+         application and should be checked into Version Control Systems.
  27
+
  28
+         -->
  29
+    <property file="ant.properties" />
  30
+
  31
+    <!-- The project.properties file is created and updated by the 'android'
  32
+         tool, as well as ADT.
  33
+
  34
+         This contains project specific properties such as project target, and library
  35
+         dependencies. Lower level build properties are stored in ant.properties
  36
+         (or in .classpath for Eclipse projects).
  37
+
  38
+         This file is an integral part of the build system for your
  39
+         application and should be checked into Version Control Systems. -->
  40
+    <loadproperties srcFile="project.properties" />
  41
+
  42
+    <!-- quick check on sdk.dir -->
  43
+    <fail
  44
+            message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'"
  45
+            unless="sdk.dir"
  46
+    />
  47
+
  48
+
  49
+<!-- extension targets. Uncomment the ones where you want to do custom work
  50
+     in between standard targets -->
  51
+<!--
  52
+    <target name="-pre-build">
  53
+    </target>
  54
+    <target name="-pre-compile">
  55
+    </target>
  56
+
  57
+    /* This is typically used for code obfuscation.
  58
+       Compiled code location: ${out.classes.absolute.dir}
  59
+       If this is not done in place, override ${out.dex.input.absolute.dir} */
  60
+    <target name="-post-compile">
  61
+    </target>
  62
+-->
  63
+
  64
+    <!-- Import the actual build file.
  65
+
  66
+         To customize existing targets, there are two options:
  67
+         - Customize only one target:
  68
+             - copy/paste the target into this file, *before* the
  69
+               <import> task.
  70
+             - customize it to your needs.
  71
+         - Customize the whole content of build.xml
  72
+             - copy/paste the content of the rules files (minus the top node)
  73
+               into this file, replacing the <import> task.
  74
+             - customize to your needs.
  75
+
  76
+         ***********************
  77
+         ****** IMPORTANT ******
  78
+         ***********************
  79
+         In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
  80
+         in order to avoid having your file be overridden by tools such as "android update project"
  81
+    -->
  82
+    <!-- version-tag: 1 -->
  83
+    <import file="${sdk.dir}/tools/ant/build.xml" />
  84
+
  85
+</project>
20  ConfigChange/FragmentBundle/proguard-project.txt
... ...
@@ -0,0 +1,20 @@
  1
+# To enable ProGuard in your project, edit project.properties
  2
+# to define the proguard.config property as described in that file.
  3
+#
  4
+# Add project specific ProGuard rules here.
  5
+# By default, the flags in this file are appended to flags specified
  6
+# in ${sdk.dir}/tools/proguard/proguard-android.txt
  7
+# You can edit the include path and order by changing the ProGuard
  8
+# include property in project.properties.
  9
+#
  10
+# For more details, see
  11
+#   http://developer.android.com/guide/developing/tools/proguard.html
  12
+
  13
+# Add any project specific keep options here:
  14
+
  15
+# If your project uses WebView with JS, uncomment the following
  16
+# and specify the fully qualified class name to the JavaScript interface
  17
+# class:
  18
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  19
+#   public *;
  20
+#}
40  ConfigChange/FragmentBundle/proguard.cfg
... ...
@@ -0,0 +1,40 @@
  1
+-optimizationpasses 5
  2
+-dontusemixedcaseclassnames
  3
+-dontskipnonpubliclibraryclasses
  4
+-dontpreverify
  5
+-verbose
  6
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
  7
+
  8
+-keep public class * extends android.app.Activity
  9
+-keep public class * extends android.app.Application
  10
+-keep public class * extends android.app.Service
  11
+-keep public class * extends android.content.BroadcastReceiver
  12
+-keep public class * extends android.content.ContentProvider
  13
+-keep public class * extends android.app.backup.BackupAgentHelper
  14
+-keep public class * extends android.preference.Preference
  15
+-keep public class com.android.vending.licensing.ILicensingService
  16
+
  17
+-keepclasseswithmembernames class * {
  18
+    native <methods>;
  19
+}
  20
+
  21
+-keepclasseswithmembers class * {
  22
+    public <init>(android.content.Context, android.util.AttributeSet);
  23
+}
  24
+
  25
+-keepclasseswithmembers class * {
  26
+    public <init>(android.content.Context, android.util.AttributeSet, int);
  27
+}
  28
+
  29
+-keepclassmembers class * extends android.app.Activity {
  30
+   public void *(android.view.View);
  31
+}
  32
+
  33
+-keepclassmembers enum * {
  34
+    public static **[] values();
  35
+    public static ** valueOf(java.lang.String);
  36
+}
  37
+
  38
+-keep class * implements android.os.Parcelable {
  39
+  public static final android.os.Parcelable$Creator *;
  40
+}
2  ConfigChange/FragmentBundle/project.properties
... ...
@@ -0,0 +1,2 @@
  1
+android.library.reference.1=../../external/ActionBarSherlock
  2
+target=android-15
BIN  ConfigChange/FragmentBundle/res/drawable-hdpi/ic_launcher.png
BIN  ConfigChange/FragmentBundle/res/drawable-ldpi/ic_launcher.png
BIN  ConfigChange/FragmentBundle/res/drawable-mdpi/ic_launcher.png
BIN  ConfigChange/FragmentBundle/res/drawable-xhdpi/ic_launcher.png
22  ConfigChange/FragmentBundle/res/layout-land/main.xml
... ...
@@ -0,0 +1,22 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3
+  android:orientation="horizontal"
  4
+  android:layout_width="fill_parent"
  5
+  android:layout_height="fill_parent"
  6
+  >
  7
+  <Button android:id="@+id/pick"
  8
+    android:layout_width="fill_parent" 
  9
+    android:layout_height="fill_parent"
  10
+    android:layout_weight="1"
  11
+    android:text="@string/pick"
  12
+    android:enabled="true"
  13
+  />
  14
+  <Button android:id="@+id/view"
  15
+    android:layout_width="fill_parent" 
  16
+    android:layout_height="fill_parent"
  17
+    android:layout_weight="1"
  18
+    android:text="@string/view"
  19
+    android:enabled="false"
  20
+  />
  21
+</LinearLayout>
  22
+
22  ConfigChange/FragmentBundle/res/layout/main.xml
... ...
@@ -0,0 +1,22 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3
+  android:orientation="vertical"
  4
+  android:layout_width="fill_parent"
  5
+  android:layout_height="fill_parent"
  6
+  >
  7
+  <Button android:id="@+id/pick"
  8
+    android:layout_width="fill_parent" 
  9
+    android:layout_height="fill_parent"
  10
+    android:layout_weight="1"
  11
+    android:text="@string/pick"
  12
+    android:enabled="true"
  13
+  />
  14
+  <Button android:id="@+id/view"
  15
+    android:layout_width="fill_parent" 
  16
+    android:layout_height="fill_parent"
  17
+    android:layout_weight="1"
  18
+    android:text="@string/view"
  19
+    android:enabled="false"
  20
+  />
  21
+</LinearLayout>
  22
+
6  ConfigChange/FragmentBundle/res/values/strings.xml
... ...
@@ -0,0 +1,6 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<resources>
  3
+    <string name="app_name">Rotation Fragment Demo</string>
  4
+    <string name="pick">Pick</string>
  5
+    <string name="view">View</string>
  6
+</resources>
97  ConfigChange/FragmentBundle/src/com/commonsware/android/rotation/fragbundle/RotationFragment.java
... ...
@@ -0,0 +1,97 @@
  1
+/***
  2
+  Copyright (c) 2012 CommonsWare, LLC
  3
+  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  4
+  use this file except in compliance with the License. You may obtain	a copy
  5
+  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  6
+  by applicable law or agreed to in writing, software distributed under the
  7
+  License is distributed on an "AS IS" BASIS,	WITHOUT	WARRANTIES OR CONDITIONS
  8
+  OF ANY KIND, either express or implied. See the License for the specific
  9
+  language governing permissions and limitations under the License.
  10
+	
  11
+  From _The Busy Coder's Guide to Android Development_
  12
+    http://commonsware.com/Android
  13
+ */
  14
+
  15
+package com.commonsware.android.rotation.fragbundle;
  16
+
  17
+import android.app.Activity;
  18
+import android.content.Intent;
  19
+import android.net.Uri;
  20
+import android.os.Bundle;
  21
+import android.provider.ContactsContract;
  22
+import android.view.LayoutInflater;
  23
+import android.view.View;
  24
+import android.view.ViewGroup;
  25
+import com.actionbarsherlock.app.SherlockFragment;
  26
+
  27
+public class RotationFragment extends SherlockFragment implements
  28
+    View.OnClickListener {
  29
+  static final int PICK_REQUEST=1337;
  30
+  Uri contact=null;
  31
+
  32
+  @Override
  33
+  public View onCreateView(LayoutInflater inflater, ViewGroup parent,
  34
+                           Bundle state) {
  35
+    View result=inflater.inflate(R.layout.main, parent, false);
  36
+
  37
+    result.findViewById(R.id.pick).setOnClickListener(this);
  38
+
  39
+    View v=result.findViewById(R.id.view);
  40
+
  41
+    v.setOnClickListener(this);
  42
+
  43
+    if (state != null) {
  44
+      String contactUri=state.getString("contact");
  45
+
  46
+      if (contactUri != null) {
  47
+        contact=Uri.parse(contactUri);
  48
+      }
  49
+    }
  50
+
  51
+    v.setEnabled(contact != null);
  52
+
  53
+    return(result);
  54
+  }
  55
+
  56
+  @Override
  57
+  public void onSaveInstanceState(Bundle outState) {
  58
+    super.onSaveInstanceState(outState);
  59
+
  60
+    if (contact != null) {
  61
+      outState.putString("contact", contact.toString());
  62
+    }
  63
+  }
  64
+
  65
+  @Override
  66
+  public void onActivityResult(int requestCode, int resultCode,
  67
+                               Intent data) {
  68
+    if (requestCode == PICK_REQUEST) {
  69
+      if (resultCode == Activity.RESULT_OK) {
  70
+        contact=data.getData();
  71
+        getView().findViewById(R.id.view).setEnabled(true);
  72
+      }
  73
+    }
  74
+  }
  75
+
  76
+  @Override
  77
+  public void onClick(View v) {
  78
+    if (v.getId() == R.id.pick) {
  79
+      pickContact(v);
  80
+    }
  81
+    else {
  82
+      viewContact(v);
  83
+    }
  84
+  }
  85
+
  86
+  public void pickContact(View v) {
  87
+    Intent i=
  88
+        new Intent(Intent.ACTION_PICK,
  89
+                   ContactsContract.Contacts.CONTENT_URI);
  90
+
  91
+    startActivityForResult(i, PICK_REQUEST);
  92
+  }
  93
+
  94
+  public void viewContact(View v) {
  95
+    startActivity(new Intent(Intent.ACTION_VIEW, contact));
  96
+  }
  97
+}
31  ConfigChange/FragmentBundle/src/com/commonsware/android/rotation/fragbundle/RotationFragmentDemo.java
... ...
@@ -0,0 +1,31 @@
  1
+/***
  2
+  Copyright (c) 2012 CommonsWare, LLC
  3
+  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  4
+  use this file except in compliance with the License. You may obtain	a copy
  5
+  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  6
+  by applicable law or agreed to in writing, software distributed under the
  7
+  License is distributed on an "AS IS" BASIS,	WITHOUT	WARRANTIES OR CONDITIONS
  8
+  OF ANY KIND, either express or implied. See the License for the specific
  9
+  language governing permissions and limitations under the License.
  10
+	
  11
+  From _The Busy Coder's Guide to Android Development_
  12
+    http://commonsware.com/Android
  13
+ */
  14
+
  15
+package com.commonsware.android.rotation.fragbundle;
  16
+
  17
+import android.os.Bundle;
  18
+import com.actionbarsherlock.app.SherlockFragmentActivity;
  19
+
  20
+public class RotationFragmentDemo extends SherlockFragmentActivity {
  21
+  @Override
  22
+  public void onCreate(Bundle savedInstanceState) {
  23
+    super.onCreate(savedInstanceState);
  24
+
  25
+    if (getSupportFragmentManager().findFragmentById(android.R.id.content)==null) {
  26
+      getSupportFragmentManager().beginTransaction()
  27
+                                 .add(android.R.id.content,
  28
+                                      new RotationFragment()).commit();
  29
+    }
  30
+  }
  31
+}

0 notes on commit bc021f4

Please sign in to comment.
Something went wrong with that request. Please try again.