3

我正在开发一个提供一组多个Button对象的接口,每个对象都附加了相同的OnClickListener. 当单击所述Buttons 时,它们应该启动一个Activity,如onClick.

这是我的参考代码:

public class Calcs extends SherlockFragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // ...

        CalcLoader buttonListener = new CalcLoader(getActivity());
        LinearLayout buttons = (LinearLayout) v.findViewById(R.id.calculatorlist); // v is the inflated View
        for (int i = 0; i < buttons.getChildCount(); i++) {
            View b = buttons.getChildAt(i);
            if (b instanceof Button) {
                ((Button) b).setOnClickListener(buttonListener);
            }
        }

        // Test Code: Location 1
        Intent i = new Intent(getActivity(), MyCalcActivity.class);
        getActivity().startActivity(i);

        // ...
    }

    private class CalcLoader implements OnClickListener {
        private Activity mOwner;

        public CalcLoader(Activity owner) {
            mOwner = owner;

            // Test Code: Location 2
            Intent i = new Intent(mOwner, MyCalcActivity.class);
            mOwner.startActivity(i);
        }

        public void onClick(View v) {
            if (v instanceof Button) {
                // Actual Code: Location 3
                Intent i = new Intent(mOwner, MyCalcActivity.class);
                mOwner.startActivity(i);
            }
        }
    }
}

尽管如此,我还是有一些奇怪的行为。在上面的代码中,我放置了一些startActivity测试,标记为位置 1 和 2。在这两种情况下,Activity启动都正确,一切都很好!

但是,在应该执行工作代码的位置 3,我从 launch 中得到了一些奇怪的行为Activity

  • 起初,Activity它启动得很好。它显示一个单一的文本字段并且它被聚焦,软键盘出现了。这是对的。
  • 现在,当我单击后退按钮时,键盘会关闭。这是对的。
  • 再次单击返回,该字段将失去焦点。这不应该发生。相反,Activity应该关闭并返回前一个。
  • 再次单击返回,整个应用程序将关闭(而不是返回到上一个Activity)。显然,这不应该发生。

重申一下,当Activity从位置 1 或 2 开始时,一切正常;后堆栈是正确的并正确返回初始值Activity

这里出了什么问题?为什么,当我Activity从 from开始时onClick,它会失败,而它在任何其他位置都可以工作?

更新:在构造函数中保存Intent并在onClick方法中重用它会产生相同的故障结果,就像Activity从 UI 线程启动一样。

第二次更新:使文本字段无法聚焦对故障没有影响;后退按钮仍然关闭应用程序。此外,在 2.3.3 模拟器中运行也有相同的结果。但奇怪的是,在第二次按下后退按钮(文本字段失去焦点)后,如果您等待约 3 秒,则Activity关闭并返回主按钮。

第三次更新:从文本字段获取焦点的后退按钮不会触发任何键事件(onKeyDown或)。onBackPressed此外,如果您在文本字段失去焦点后进行交互Activity,它会显示加载Activity相同类型的新内容的动画,但此处也存在故障。

这似乎是操作系统级别的问题,在 Android 2.2 (API 8)、2.3.1 (API 9) 和 2.3.3 (API 10) 中发现。Eclair (API 7) 和 API 11+ 没有这个问题。在这一点上,我相信我正在寻找某种解决方法......

4

1 回答 1

0

Turns out my issue was not caused by something I detailed in my original post, so I apologize for that.

The issue was caused by a SurfaceView item on a tab unrelated to the tab I was testing on. After rebuilding the tabs and layouts from the ground up (and building each time), I discovered the lag-back-glitch was only caused when a SurfaceView was present in a non-focused tab.

I finally found that I was not the only one with this issue.

To solve:

  • Created an onPause method in the main Activity. In here, I destroy the SurfaceView using container.removeView(..);.
  • Created an onResume method in the main Activity. In here, I inflate the SurfaceView from a new XML file containing ONLY the SurfaceView item, and add it to the original container.
  • Lastly, implemented a android.view.SurfaceHolder.Callback in the SurfaceView to erase the contents of the surface before it is removed.

It stumps me that this happens only on APIs 8-10, but I'm glad it's solved now. Kudos to everyone that offered their assistance!

于 2012-07-09T21:53:10.350 回答