简体   繁体   English

通过后退按钮从子片段导航回到父活动

[英]Navigate via back-button from child fragment back to parent activity

I have a main activity and 2 fragments. 我有一个主要活动和2个片段。 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. 我有一个活动(带有导航抽屉)和2个片段。 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 . 但是,当我单击“后退”按钮时,我总是希望返回到main_activity.xml

When you navigate through Activities, the best way is to use Intent s like this (if I understood your question): 当您浏览“活动”时,最好的方法是使用这样的Intent (如果我理解您的问题):

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");
        }
    }

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. 您必须按标签查找片段,并检查其是否为null,如果为null,则替换该片段,或者如果片段被隐藏,则再次显示该片段。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM