简体   繁体   English

合成Android片段backstack

[英]Synthesize Android fragment backstack

I have an activity that uses fragments to change views instead of launching new activities. 我有一个活动,它使用片段来更改视图而不是启动新活动。 Lets say I have 3 fragments A, B and C. When the application launches the default fragment is set to A. The user can click a button on A to transition to B -- same with B to C. 假设我有3个片段A,B和C.当应用程序启动时,默认片段设置为A.用户可以单击A上的按钮转换到B - 与B到C相同。

Therefore the backstack looks like: [A] -> [B] -> [C] 因此,后台堆栈看起来像:[A] - > [B] - > [C]

What I need to do is deep link directly to fragment C from a notification while still building out the backstack so that when the activity is launched. 我需要做的是直接从通知中直接链接到片段C,同时仍然构建backstack,以便在启动活动时。 Fragment C should be displayed while allowing the user to click the back button to get back to views B and A respectively. 应显示片段C,同时允许用户单击后退按钮分别返回视图B和A.

You can make 3 separate transactions. 您可以进行3次单独的交易。 This is a lot more natural than manually checking the state of the backstack. 这比手动检查Backstack的状态要自然得多。

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    showFragmentA();

    if (getIntent().hasExtra("some_deep_link_flag")) {
        showFragmentB();
        showFragmentC();
    }
}

private void showFragmentA() {
    Fragment a = new Fragment();

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, a)
            .addToBackStack(null)
            .commit();
}

private void showFragmentB() {
    Fragment b = new Fragment();

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, b)
            .addToBackStack(null)
            .commit();
}

private void showFragmentC() {
    Fragment c = new Fragment();

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, c)
            .addToBackStack(null)
            .commit();
}

If the stack is always going to A -> B -> C then you could override onBackPressed() in the activity and pop the back stack and check to see what the situation in the fragment stack is. 如果堆栈总是转到A - > B - > C,那么你可以覆盖活动中的onBackPressed()并弹出后台堆栈并检查片段堆栈中的情况。

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getFragmentByTag("C") != null) {
        if (getSupportFragmentManager().getBackStackEntryCount() == 2) {
            getSupportFragmentManager().beginTransaction.replace(...).commit();
        } else {
            getSupportFragmentManager().popBackStack();
        }
    } else if (getSupportFragmentManager().getFragmentByTag("B") != null) {
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            getSupportFragmentManager().beginTransaction.replace(...).commit();
        } else {
            getSupportFragmentManager().popBackStack();
        }
    } else {
        //You are already on Fragment A
        super.onBackPressed();
    }
}

Like jmcdonnell40 suggests: 像jmcdonnell40建议:

My guess is you should put an extra on the notification intent which makes clear that you are entering your Activity from the notification. 我的猜测是你应该在通知意图上加上额外的内容,这表明你是从通知中输入你的活动。

Then, in the activity's oncreate, check for that extra, and if it is present, just do the 2 necessary fragment transactions (fragment A -> B -> C) and add them to the backstack. 然后,在activity的oncreate中,检查该额外的,如果它存在,只需执行2个必要的片段事务(片段A - > B - > C)并将它们添加到backstack。

you're going to have to open your last fragment manually anyway. 无论如何,你将不得不手动打开你的最后一个片段。

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

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