简体   繁体   English

一项活动有多个家长活动?

[英]Having multiple parent activities for one activity?

So I have two separate activities in my app that both have a 'Contact' button on them. 因此,我的应用程序中有两个单独的活动,两个活动上都有一个“联系”按钮。 Clicking on either button on both activity launches the same 'ContactActivity'. 在两个活动上单击任一按钮将启动相同的“ ContactActivity”。 Now problem is when I want to implement up navigation for this activity, how would I do it for both depending on activity I'm on? 现在的问题是,当我想为该活动实施向上导航时,如何根据我正在进行的活动对两者进行导航?

Reason being is because when on ContactActivity, and hitting up navigation I'm brought back to the parent activity defined, in this case two activities are technically the parent depending on which you call the Contact intent from. 原因是因为在使用ContactActivity并进行导航时,我带回到了定义的父活动,在这种情况下,从技术上讲,取决于您从中调用Contact意图的方式,两个活动是父活动。 I know that I could just duplicate the ContactActivity into another, and use separate ones, but I just feel that there could be a simpler way of doing this. 我知道我可以将ContactActivity复制到另一个,并使用单独的,但我只是觉得可以有一种更简单的方法。

Maybe it's not ideal, perhaps you can pass a parameter in the intent you use to run the child activity that you can evaluate on onBackPressed(). 也许这不是理想的选择,也许您可​​以在意图中传递参数来运行可以在onBackPressed()上评估的子活动。

My reasoning is: A and B are C's parent activities. 我的推理是:A和B是C的父级活动。 If you are in A and call C, you can do: 如果您在A中并致电C,则可以执行以下操作:

Intent intent = new Intent(this, C.class);
intent.putExtra("parent", "A")

In C you get that parameter: 在C中,您可以得到该参数:

mParentActivity = intent.getStringExtra("parent");

Then in onBackPressed(): 然后在onBackPressed()中:

public void onBackPressed() {
        if(mParentActivity.equals("A")) {
            Intent intent = new Intent(this, A.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else if (mParentActivity.equals("B")) {
            Intent intent = new Intent(this, B.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        } else {
            super.onBackPressed();
        }
    }

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

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