0

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

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

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

更清晰:

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

4

3 回答 3

1

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

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
于 2015-05-15T14:13:45.310 回答
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");
        }
    }
于 2015-05-15T14:25:15.290 回答
0

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

    @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();
        }
}
于 2015-05-15T14:25:31.337 回答