如何从另一个活动转到活动的片段?

时间:2016-08-30 12:31:10

标签: android android-fragments

我有一个DrawerMenuActivity假设" AA"它有3个片段" A"," B"和" C"作为它的菜单。片段" A"是我的默认屏幕。现在我已经导航到片段" B"其中包含listview项目,每个项目点击将打开另一个新活动" AB"。在AB中会有一个按钮,按下该按钮将打开另一个活动" AC"。所以基本上流程如下,

来自AA(DrawerMenuActivity) - > " A" fragemnt - > " B"或" C"片段 - > AB(活动) - > AC(活动)

现在我要做的就是回到片段" B"来自活动" AC"。

我已经取代了Activity" AA"像这样,

在OnCreate()中

if (savedInstanceState == null) {
            A fragment = new A();
            replaceFragment(fragment, false, Constants.HOME_FRG_TAG);
            Handler handler = new Handler(Looper.getMainLooper());
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    if (getIntent().getStringExtra("FROM AC") != null) {
                        updateDisplay(1);
                    }
                }
            };
            handler.postDelayed(runnable, 10);
        }

并且用于替换片段,

public void updateDisplay(int position) {
        Fragment fragment = null;
        String tag = null;
        switch (position) {
            case 0:
                clearStack();
                mDrawerLayout.closeDrawer(mDrawerList);
                break;
            case 1:
                fragment = new B();
                tag = "B";
                break;
            case 2:
                fragment = new C();
                tag = "C";
                break;
            default:
                break;
        }

        if (fragment != null) {
            replaceFragment(fragment, true, tag);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            Log.e("Activity", "Error in creating fragment");
        }
    }

replaceFragment()函数,

public void replaceFragment(Fragment newFragment, boolean backStackTag, String fragmentTag) {
        try {
            Fragment selectedFrag = getSupportFragmentManager().findFragmentByTag(fragmentTag);
            if (selectedFrag != null && selectedFrag.isVisible()) {
                return;
            }
            FragmentManager fm = AA.this.getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            if (backStackTag) transaction.addToBackStack(null);
            transaction.commit();
        } catch (Exception e) {
            Log.e("exception ", " on adding fragment");
            // TODO: handle exception
        }
    }

和clearStack(),

public void clearStack() {
        FragmentManager fm = AA.this.getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
在AC活动中,我试图回到片段" B"像这样,

Intent in = new Intent(AC.this, AA.class);
                in.putExtra("FROM AC", "from ac");
                startActivity(in);

现在这就是我打算做的事情,但问题是它何时加载片段" A"打开一小段时间,然后它回到片段" B"。

如何导航到片段" B"来自Activity" AC"正确?请任何帮助将不胜感激...... !!

2 个答案:

答案 0 :(得分:1)

使用startActivityForResult将params从AC传递给AA。它有点复杂.EventBus可能对你有所帮助。

答案 1 :(得分:1)

首先从AB到AC启动它,就像startActvityForResult一样,并确保没有传递清晰的backstack标志。就这样做:

public static final int REQ_CODE = 1;

Intent in = new Intent(this, AB.class);
startActivityForResult(in, REQ_CODE);

所以后备箱没有清理。在AA中替换片段时,请执行以下操作:

public void replaceFragment(Fragment newFragment, boolean backStackTag, String fragmentTag) {
        try {
            Fragment selectedFrag = getSupportFragmentManager().findFragmentByTag(fragmentTag);
            if (selectedFrag != null && selectedFrag.isVisible()) {
                return;
            }
            FragmentManager fm = AA.this.getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            transaction.replace(R.id.container, fragmentB).commit();
        } catch (Exception e) {
            Log.e("exception ", " on adding fragment");
            // TODO: handle exception
        }
    }

如果您想从活动AC返回,如果您想要导航回AA,则将结果设置为RESULT_OK,否则设置为RESULT_CANCELED。

setResult(Activity.RESULT_OK);
finish();

在AB中,覆盖onActivityResult方法,使其返回AA:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQ_CODE) {
        if (resultCode == RESULT_OK) {
            finish();
        }
    }
}

因此,如果通过RESULT_OK,它将返回到AA,其中活动仍在onStop()中并且状态已保存。另外,请务必在onCreate而不是onStart中初始化您的观看次数。