AddToBackStack () doesn't work. Return to main page not previous fragment
I am new to Android. When I try to use addToBackStack()
I am facing a problem.
When I press the back button in Fragment 2, it doesn't go to Fragment 1, but home.
Why? Is there something wrong with my code? Thank!
MaiinActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager mFragmentManager = getFragmentManager();
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.add(R.id.id_frame, new firstFrag(), "firstFrag");
mFragmentTransaction.commit();
}
}
activity_main.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myfragtest2.MainActivity" >
<FrameLayout
android:id="@+id/id_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></FrameLayout>
</RelativeLayout>
firstFrag.java
public class firstFrag extends Fragment {
private Button button1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_frag, container, false);
button1 = (Button)view.findViewById(R.id.btn1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager myFragmentManager = getFragmentManager();
FragmentTransaction myFTransaction = myFragmentManager.beginTransaction();
myFTransaction.replace(R.id.id_frame, new secondFrag());
myFTransaction.addToBackStack(null);
myFTransaction.commit();
}
});
return view;
}
}
first_frag.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" >
<EditText
android:id="@+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is frag 1!!"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="move to frag 2"
/>
</LinearLayout>
source to share
Instead, null
use the tag name in addToBackStack()
, in both snippets oncall
. I used the following code, it works for me:
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.add(R.id.mainfragment, new Fragment1(), "Fragment1");
ft.addToBackStack("Fragment1");
ft.commit();
source to share
As mentioned earlier, you must use the addToBackStack (String name) method before committing. But the real problem is that your MainActivity is extending ActionBarActivity. This appears to override the behavior of the Back button. Try extending Activity. Should work fine. In this case, you have to deal with possible disruptions in action.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.add(R.id.container_main, new ListFragment())
.commit();
}
public void startDetailFragment (View view) {
getFragmentManager().beginTransaction()
.replace(R.id.container_main, new DetailFragment())
.addToBackStack(null)
.commit();
}
source to share