I'm trying to learn fragments on android and my code is not working.
FragmentCustomAnimations contains MainActivity.
What i want is the frame MainActivity to change when i push the button on MainActivity.
The error message on logcat is Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.FragmentCustomAnimations for onClick handler on view class android.widget.Button with id 'button1'
Eventhough there is no function sendmessage in all my file.
FYI, its working when i put the button on FragmentCustomAnimations.
Is it impossible to use the button on fragment to trigger action to change the fragment itself?
Here is the code:
FragmentCustomAnimations.java
package com.example.myfirstapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentCustomAnimations extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_stack);
if (savedInstanceState == null) {
Fragment newFragment = InitialFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
public static class InitialFragment extends Fragment {
static InitialFragment newInstance() {
InitialFragment f = new InitialFragment();
Bundle args = new Bundle();
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_main, container, false);
return v;
}
}
}
fragment_stack.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="4dip"
android:gravity="center_horizontal"
android:background="#drawable/window2x"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout
android:id="#+id/simple_fragment"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
MainActivity.java
package com.example.myfirstapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button inner = (Button) findViewById(R.id.button1);
inner.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
getActionBar().setDisplayShowHomeEnabled(false);
return super.onCreateOptionsMenu(menu);
}
#Override
public void onClick(View v) {
addFragmentToStack();
}
void addFragmentToStack() {
Fragment newFragment = CountingFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(R.animator.fragment_slide_left_enter,
R.animator.fragment_slide_left_exit,
R.animator.fragment_slide_right_enter,
R.animator.fragment_slide_right_exit);
ft.replace(R.id.simple_fragment, newFragment);
ft.addToBackStack(null);
ft.commit();
}
public static class CountingFragment extends Fragment {
static CountingFragment newInstance() {
CountingFragment f = new CountingFragment();
Bundle args = new Bundle();
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_display_message, container, false);
return v;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="?android:attr/actionBarSize"
android:layout_gravity="center_vertical"
tools:context=".MainActivity" >
<EditText
android:id="#+id/edit_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/edit_message"
android:layout_gravity="center_vertical"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/editTextEmail"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/editTextPassword"
android:inputType="textPassword" />
<Button
android:id="#+id/button1"
style="#style/CodeFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="#drawable/custom_button"
android:text="#string/button_send" />
</LinearLayout>
Related
The fragment shows up by default but I wanted it to be hidden/invisible until I click this button. The problem that I am experiencing is that when I click this button, the fragment won't show up. I'm a complete beginner when it comes to fragments.
My codes in my Main Activity:
package com.example.julian.tutorial_fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View frag = findViewById(R.id.fragment3);
frag.setVisibility(View.GONE);
}
//This gets called by the TopFragment when the user clicks the button
#Override
public void createMeme(String top, String bottom) {
BottomPictureFragment bottomFragment = (BottomPictureFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomFragment.setMemeText(top, bottom);
}
//this is the onButtonClick
public void sample(View view) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_Search hello = new Fragment_Search();
fragmentTransaction.add(R.id.fragment3, hello, "HELLO");
fragmentTransaction.commit();
}
}
My codes in Fragment_Search:
package com.example.julian.tutorial_fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Julian on 8/17/2017.
*/
public class Fragment_Search extends Fragment {
public interface Fragment_SearchInterface {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_layout, container, false);
return view;
}
}
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.julian.tutorial_fragment.MainActivity">
<RelativeLayout
android:layout_width="377dp"
android:layout_height="498dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<fragment
android:id="#+id/fragment2"
android:name="com.example.julian.tutorial_fragment.BottomPictureFragment"
android:layout_width="250dp"
android:layout_height="250dp"
tools:layout="#layout/bottom_picture_fragment"
tools:layout_editor_absoluteX="67dp"
tools:layout_editor_absoluteY="223dp"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<fragment
android:id="#+id/fragment"
android:name="com.example.julian.tutorial_fragment.TopSectionFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/top_section_fragment"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fragment2"
android:layout_alignStart="#+id/fragment"
android:text="Button"
android:onClick="sample"/>
<fragment
android:id="#+id/fragment3"
android:name="com.example.julian.tutorial_fragment.Fragment_Search"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="#+id/button2"
android:layout_marginTop="92dp"
android:layout_toStartOf="#+id/button2" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
change your sample function and add this
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.your_layout, new Fragment_Search())
.addToBackStack(null)
.commit();
I have a fragment(A) , where another fragment(B) is being opened . When I press back button it just refreshes the fragment(B) instead of exiting from it and returning to fragment A.
I tried poping back the fragment A in the Activity's onBackPressed callback method but it didn't change a thing :
#Override
public void onBackPressed() {
if(B.active)
{
mFragmentManager.popBackStack( A.TAG , 0);
B.active = false;
}
}
** the active boolean is just something I added as part of the solution. It's initialized to TRUE once the fragment is instantiated.
I don't know how to commit a project!
package com.example.a;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/9.
*/
public class ActivityA extends AppCompatActivity{
#InjectView(R.id.c)
LinearLayout c;
#InjectView(R.id.btn)
TextView btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
ButterKnife.inject(ActivityA.this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.e, FragA.newInstance(), "A");
// fragmentTransaction.addToBackStack("A");
fragmentTransaction.commitAllowingStateLoss();
}
});
}
}
package com.example.a;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/10.
*/
public class FragA extends Fragment{
private View rootView;
public static FragA newInstance() {
return new FragA();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FragA() {
}
#InjectView(R.id.a)
LinearLayout a;
#InjectView(R.id.btn)
TextView btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fraga,container,false);
ButterKnife.inject(this,rootView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.e, FragB.newInstance(), "B");
fragmentTransaction.addToBackStack("B");
fragmentTransaction.commitAllowingStateLoss();
}
});
return rootView;
}
}
package com.example.a;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by 77930 on 2015/11/10.
*/
public class FragB extends Fragment{
private View rootView;
public static FragB newInstance() {
return new FragB();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FragB() {
}
#InjectView(R.id.b)
LinearLayout b;
#InjectView(R.id.btn)
TextView btn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragb,container,false);
ButterKnife.inject(this, rootView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStackImmediate();
}
});
return rootView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/c"
android:background="#4b14b1"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/e"
android:layout_width="match_parent"
android:layout_height="300dp">
</FrameLayout>
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="c"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/a"
android:background="#4b14b1"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="A"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:id="#+id/b"
android:background="#4b14b1"
android:layout_height="match_parent">
<TextView
android:id="#+id/btn"
android:textColor="#ffffff"
android:textSize="100dp"
android:gravity="center"
android:text="B"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
I am new to android and learning fragment instead of multiple, i have problem. when i call planet fragment from friends "java.lang.IllegalStateException: Activity has been destroyed" android runtime FATAL exception occurs here is my code. i saw this page but couldn't figure out how to use it. I will be grateful for any help
fragment_planet.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
Friends.java
package com.app.hubara;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Friends extends Fragment {
public Friends() { }
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.content_friends, container, false);
MainActivity main = new MainActivity();
main.friends();
return rootView;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="100dp"/>
<Button
android:id="#+id/display"
android:height="70dp"
android:width="300dp"
android:layout_marginTop="0dp"
android:text="Display"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity
package com.app.hubara;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import java.util.Locale;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.display);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Friends frag_friends = new Friends();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, frag_friends);
fragmentTransaction.commit();
}
});
}
public void friends(){
PlanetFragment planetFragment = new PlanetFragment();
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, planetFragment);
fragmentTransaction.commit();
}
public static class PlanetFragment extends Fragment {
public PlanetFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt("Earth");
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", "Earth");
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
To access an Activity, you shouldn't do this:
MainActivity main = new MainActivity();
main.friends();
Because this destroys current Activity then instantiates another new Activity.
You can access it appropriately by calling getActivity():
MainActivity main = getActivity();
main.friends();
Note that actually you are recommended to define and use an interface to communicate between Fragment and Activity. If you want to learn further, please refer to the official training.
I have 2 fragments . In the first fragment there is a button which on clicking will move to 2 nd fragment. When the screen is rotated in this 2nd fragment,after orientation change I see first fragment instead of 2nd fragment. Can anyone help me in solving this issue?
The code is as below :
Activity code:
package com.andr.fragmentsorientationdemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
public class OrientationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_orientation);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragmentone()).commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.orientation, menu);
return true;
}
}
<br>Code for FragmentOne <br>
=============================
package com.andr.fragmentsorientationdemo;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragmentone extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
Button b = (Button)v.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragmenttwo()).addToBackStack(null).commit();
}
});
return v;
}
}
<br>code for Fragmenttwo
===================
package com.andr.fragmentsorientationdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* #author 245742
*
*/
public class Fragmenttwo extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_two, container, false);
Button b = (Button)v.findViewById(R.id.button1);
return v;
}
}
<br>layout file for fragmentone <br>
=====================================
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<br> Layout for fragmenttwo
==============================
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2222222222" />
</LinearLayout>
Can anyone help me in solving this issue.
Try this-
FragmentManager fragmentManager =null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_orientation);
if(fragmentManager==null){
fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, new Fragmentone()).commit();
}
}
or else
use this in manifest's activity tag-
android:configChanges="orientation"
I have an activity which displays two fragments:
When activity is created, it displays Fragment1.
When the user presses the button on Fragment1, it displays Fragment2, adding to backstack.
Fragments are straightforward. Fragment1 contains CheckBox and EditText. Fragment2 contains simple TextView. Also I call setRetainInstance(true) in fragment's onCreate(...) method.
Problem: Fragment1 loses its state if Fragment2 is displayed and device is rotated twice. However, everything works as expected if device is rotate only once.
Steps to reproduce:
Launch application.
Check CheckBox and type some text into EditText
Press Button to launch Fragment2
Rotate device
Rotate device once again
Press Back button on your device (or ESC on Emulator) to return back to Fragment1
Fragment1 lost its state (checkbox is unchecked and EditText is empty).
However, if you return back to Fragment1 after step#4 - Fragment1 keeps its state as expected.
Where is the problem? All code is below, including layouts.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment1.xml:
<?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" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Fragment2" />
</LinearLayout>
fragment2.xml:
<?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="Fragment #2" />
</LinearLayout>
FragmentTestActivity.java:
package fragmenttest.example.com;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
public class FragmentTestActivity extends Activity implements FragmentListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(Fragment1.TAG) == null) {
fm.beginTransaction().replace(R.id.placeholder, new Fragment1(), Fragment1.TAG).commit();
}
}
#Override
public void onButtonClick() {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(Fragment2.TAG) == null) {
fm.beginTransaction().replace(R.id.placeholder, new Fragment2(), Fragment2.TAG).addToBackStack(Fragment2.TAG).commit();
}
}
}
Fragment1.java:
package fragmenttest.example.com;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
public class Fragment1 extends Fragment {
public static final String TAG = Fragment1.class.getName();
private FragmentListener mListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment1, container, false);
Button button = (Button) root.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mListener.onButtonClick();
}
});
return root;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mListener = (FragmentListener) activity;
}
}
Fragment2.java:
package fragmenttest.example.com;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
public static final String TAG = Fragment2.class.getName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment2, container, false);
return root;
}
}
FragmentListener.java:
package fragmenttest.example.com;
public interface FragmentListener {
public void onButtonClick();
}
It may be because onCreate isn't called when restoring the fragment.
See:http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)