通过后退按钮从子片段返回父片段 activity

Navigate via back-button from child fragment back to parent activity

我有一个主要 activity 和 2 个片段。在我的主要 activity 中,我有导航抽屉,可以在片段中导航。我这样处理后退按钮:

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

在两个片段之间导航后,当我点击后退按钮时,我从后台堆栈中获取了最后一个片段。但是,从片段导航到另一个片段应该是不可能的。

更清晰:

我有一个 Activity(带导航抽屉)和 2 个片段。从导航抽屉中,我可以根据需要多次访问这两个片段。但是当我按下后退按钮时,我总是想回到 main_activity.xml.

当您浏览活动时,最好的方法是像这样使用 Intents(如果我理解您的问题):

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
**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");
        }
    }

您必须通过标签找到片段并检查它是否为空,如果为空则替换该片段或者如果片段被隐藏则再次显示该片段。

    @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();
        }
}