lost.in.code
lost.in.code

Reputation: 27

Fragment Navigation between fragments

I'am new in Android and I work with fragments. I have activity (MainActivity) and 3 Fragments, A->B->C. I need to back navigate from C fragment to B, from B to A and backward. I was cliked on Fragment A see A, clicked on B see B, clicked on C see C, but when I clicked Back my app was closed. Why ? Here is a example what I call fragments

Fragment Subcategory = new Subcategory();
            Bundle bundle = new Bundle();
            bundle.putInt("id", i);
            String title = dba.getTitle(i,true);
            bundle.putString("title", title);
            Subcategory.setArguments(bundle);
            FragmentTransaction transaction_cat = getFragmentManager().beginTransaction();

            transaction_cat.replace(R.id.fragment_container, Subcategory);
            transaction_cat.addToBackStack(null);
            transaction_cat.commit();

I can't found any simple example which show me how to navigate between fragments, and after reading this article I don't understand how it works. Tell me please, and write example how I must do this.

Upvotes: 0

Views: 1603

Answers (1)

Yushi
Yushi

Reputation: 93

I think you can try override method onBackPressed() for the activity.

update:

Here is part of my code.

public class MainActivity extends Activity {

    RelativeLayout mContainer;

    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContainer = (RelativeLayout) findViewById(R.id.fragment_container);
        addFragment();
    }

    private void addFragment() {
        FragmentManager fm = getFragmentManager();
        Fragment f = fm.findFragmentById(R.id.fragment_container);
        if (f == null) {
            f = new MainActivityFragment();
            f.setArguments(getIntent().getExtras());
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.fragment_container, f);
            ft.addToBackStack("" + count++);
            ft.commit();
        }
    }

    private void replaceFrament() {
        FragmentManager fm = getFragmentManager();
        Fragment f;
        f = new BlankFragment();
        f.setArguments(getIntent().getExtras());
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragment_container, f);
        ft.addToBackStack("" + count++);
        ft.commit();
    }

    public void onAddFragmentClick(View v) {
        replaceFrament();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}

<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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivityFragment">
    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></RelativeLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Fragment"
        android:layout_alignParentBottom="true"
        android:onClick="onAddFragmentClick"/>

</RelativeLayout>

Upvotes: 1

Related Questions