I'm trying to use setRetainInstance (), but it doesn't seem to work for me! equals sign I simplified the problem to the easiest version:
I have a snippet with TextView and Button.
Source TextView - "I'm waiting", when the button is pressed, the text changes to "Hello!"
This is my code:
Fragment
public class SayHelloFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.say_hello_layout, container); final TextView text = (TextView) view.findViewById(R.id.textView1); view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { text.setText("Hello!"); } }); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setRetainInstance(true); } }
Activity:
public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Action Layout:
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/say_hello_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" class="com.example.retaintext.SayHelloFragment" />
Fragment Layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I'm Waiting" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
But when I play it, press the button and change the orientation of the device, the text will be reset.
What am I missing? How can i solve this? Thanks!
java android android-fragments
Addev
source share