0

I have a main activity and 2 fragments. In my main activity I have navigation drawer that navigates through the fragments. I handle the back button pressed like this:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStackImmediate();
    } else {
        super.onBackPressed();
    }
}

After navigating between the two fragments, when I hit the back button, I get last fragment from backstack. However, navigating from fragment to another fragment should not be possible.

More Clarity:

I have one Activity (with a navigation drawer) and 2 Fragments. From the navigation drawer I can go to both fragments as many times as I want. But when I hit the back button, I want always to go back to the main_activity.xml.

Redson
  • 2,098
  • 4
  • 25
  • 50
  • *what I want to do is return to my man activity from any fragment at all times.*, if you are not starting a new Activity, you are always on your `MainActivity` – Blackbelt May 15 '15 at 14:12
  • @Blackbelt Sorry, what I meant was to return to `main_activity.xml`. Edited the question – Redson May 15 '15 at 14:13
  • So...you only have one activity. And you want to return to this activity, which you are still on. Doesn't make sense to me :( You ARE still in the main_activity.xml. If you want to get the same let's say 'state' as in the beginning, call your activity from the fragments to do that. – JacksOnF1re May 15 '15 at 14:17
  • @JacksOnF1re See the updated question. I hope thats more clear – Redson May 15 '15 at 14:18

3 Answers3

1

When you navigate through Activities, the best way is to use Intents like this (if I understood your question):

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • I have 1 Activity (with a navigation drawer) and 2 Fragments. From the navigation drawer I can go to both fragments as many times as I want. But when I hit the back button, I want to go back to the `main_activity.xml` instead popping from the stack – Redson May 15 '15 at 14:17
  • try to create one more fragment which is the Home fragment and put there everything you have on the main_activity.xml and you can go back to it from the navigation drawer. I think that when you have Fragments in an activity, you can not have layouts outside them – Gabriella Angelova May 15 '15 at 14:18
  • Cool, thanks for the response. Helped me come up with a solution. +1 and accepted – Redson May 15 '15 at 14:48
  • @Alias could you post your solution? –  Sep 13 '15 at 09:23
0
**OnCreate() :-**


public boolean onKeyDown(int keyCode, KeyEvent event)
 {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent returnIntent = new Intent();
            setResult(RESULT_CANCELED, returnIntent);
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);

LayoutInflater mInflater = LayoutInflater.from(this);

View mCustomView = mInflater.inflate(R.layout.actionbar_category_feed_list, null);

        RelativeLayout rrBack = (RelativeLayout) mCustomView.findViewById(R.id.rr_back);
        TextView title = (TextView) mCustomView.findViewById(R.id.txt_title);
        title.setText("");
        title.setTypeface(typeFaceMedium);
        rrBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mTracker.send(new HitBuilders.EventBuilder().setCategory("" + userUsername).setAction("On Search Feed Screen").setLabel("Pressed Back Button").build());
                Intent returnIntent = new Intent();
                setResult(RESULT_CANCELED, returnIntent);
                finish();
            }
        });
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

**actionbar_category_feed_list.xml**

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@android:color/white" >

    <RelativeLayout
        android:id="@+id/rr_back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" >

        <ImageView
            android:id="@+id/img_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:focusable="false"
            android:src="@drawable/back_red" />
    </RelativeLayout>

    <com.converza.library.CustomTextView
        android:id="@+id/txt_title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/rr_back"
        android:gravity="center_vertical"
        android:paddingLeft="8dp"
        android:text="@string/app_name" />

</RelativeLayout>



**At last in your fragment Activity use a OnactivityResult() for get a Returnintent from Activity.**

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Const.FEEDDETAIL && resultCode == Activity.RESULT_OK) {
            Log.e(tag, "DatashBoard Const.FEEDDETAIL RESULT_OK");
        } else if (requestCode == Const.FEEDDETAIL && resultCode == Activity.RESULT_CANCELED) {
            Log.e(tag, "DatashBoard Const.FEEDDETAIL RESULT_CANCELED");
        }
    }
InsaneCat
  • 2,115
  • 5
  • 21
  • 40
0

You have to find the fragment by tag and check if it is null or not, if it is null then replace that fragment or show the fragment again if fragment is hidden.

    @Override
    public void onBackPressed() {
             Fragment fr = getSupportFragmentManager()
                .findFragmentByTag("Fragment_Tag");

        if (fr == null) {
            getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container,
                        new Main_Fragment(), "Fragment_Tag").commit();
        } else {
            finish();
        }
}
Surender Kumar
  • 1,123
  • 8
  • 15