屏幕的 Activity 会调用自身进行类似的用途 - 但返回会退出应用程序

发布于 2024-11-01 17:30:57 字数 1897 浏览 1 评论 0原文

我有一个代表用于两种变体的屏幕的活动。唯一的区别是,在一种情况下,它用于处理另一种情况下的颜色数字。这就是它的声明方式:

> public class MainScreen extends Activity {
    /** Called when the activity is first created. */

    private Integer activityCode; 
    private static final int ACTIVITY_NUMBER = 0;
    private static final int ACTIVITY_COLOR = 1;

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

            activityCode = savedInstanceState != null ? savedInstanceState
                    .getInt("Task") : null;
            if (activityCode == null) {
                Bundle extras = getIntent().getExtras();
                activityCode = extras != null ? extras.getInt("Task") : null;
            }   
            do stuff depending on which activity is actually chosen
        }

这就是它的调用方式

从自身内部

> @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        Intent i;
        switch (item.getItemId()) {
        case OTHER_PAGE_ID:
            i = new Intent(this, MainScreen.class);
            if (activityCode == ACTIVITY_NUMBER) {
                i.putExtra("Task", ACTIVITY_COLOR);
                startActivityForResult(i, ACTIVITY_COLOR);
                finish();
            } else {
                i.putExtra("Task", ACTIVITY_NUMBER);
                startActivityForResult(i, ACTIVITY_NUMBER);
                finish();
            }

            return true;

        ....

        }

这种重用同一个类的方式真的可以吗?我对非常相似的屏幕使用相同的类,并希望根据用户的选择来回切换。

但是每次在 NUMBERS <-> 之间选择不同的屏幕时,该类都会调用自己。颜色。

问题是,当我从数字转到颜色,然后按后退箭头时,应用程序退出。但是,当我从 NUMBERS 转到另一个屏幕并按返回时,它会再次返回到 NUMBERS。

为什么在类调用自身的情况下不回到我下班的地方?我假设它只是将每个调用放入堆栈并返回。

这难道不是一个迷你递归,其中 NUMBER 将自身称为 COLOR,完成后再次出现吗?

我希望我能说清楚。感谢您的帮助

I have one activity representing a screen that is used in two variations. The only difference is that in one case it's used to handle numbers in the other for colors. This is how it is declared:

> public class MainScreen extends Activity {
    /** Called when the activity is first created. */

    private Integer activityCode; 
    private static final int ACTIVITY_NUMBER = 0;
    private static final int ACTIVITY_COLOR = 1;

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

            activityCode = savedInstanceState != null ? savedInstanceState
                    .getInt("Task") : null;
            if (activityCode == null) {
                Bundle extras = getIntent().getExtras();
                activityCode = extras != null ? extras.getInt("Task") : null;
            }   
            do stuff depending on which activity is actually chosen
        }

And this is how it is called

FROM WITHIN ITSELF

:

> @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        Intent i;
        switch (item.getItemId()) {
        case OTHER_PAGE_ID:
            i = new Intent(this, MainScreen.class);
            if (activityCode == ACTIVITY_NUMBER) {
                i.putExtra("Task", ACTIVITY_COLOR);
                startActivityForResult(i, ACTIVITY_COLOR);
                finish();
            } else {
                i.putExtra("Task", ACTIVITY_NUMBER);
                startActivityForResult(i, ACTIVITY_NUMBER);
                finish();
            }

            return true;

        ....

        }

Is this way of re-using the same class actually OK? I use the same class for very similar screens and want to switch back and forth depending on the user selection.

BUT the class calls itself everytime a different screen is selected between NUMBERS <-> COLOR.

The problem is, that when I go from NUMBERS to COLORS and then press back-arrow the app quits. However, when I go from NUMBERS to another screen and press back, it goes back to NUMBERS again.

Why doesn't going back to where I come from work in the case where the class calls itself? I would assume it just puts each call on the stack and comes back to it.

Isn't this just a mini recursion where NUMBER calls itself as COLOR and when finished appears again?

I hope I could make myself clear. Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残月升风 2024-11-08 17:30:57

这让我头晕。为什么不只使用两个Activity?如果是为了代码重用,只需使用一个包含所有公共代码的基类,然后为您的 Color 和 Number 类扩展它:

public class Base extends Activity {
    // common code here
}

public class Colour extends Base {
    // colour specific code here
}

public class Number extends Base {
    // number specific code here
}

That makes my head spin. Why not just use two Activities? If it's for code reuse, just have one base class with all your common code, then extend it for your Colour and Number classes:

public class Base extends Activity {
    // common code here
}

public class Colour extends Base {
    // colour specific code here
}

public class Number extends Base {
    // number specific code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文