1

this is my first post and I have written it after scanning for some "likely proposed" solutions for "onBackPressed" and "onKeyDown" problems. I Think they don't fit for my problem.

But enlight me if I am wrong (sorry for that in case).

Ok, here is my simple problem.

I have 2 activites : A : Starter Activity (normal Activity) B : MainMenu Activity (TabActivity)

Splash activity (A) does some init stuff and some checks. If it is satisfied, it simply calls the intent for the main menu Acitvity (B).

Activity B just fills a tabHost for 4 sub activities.

Ok so far it's all fine. But

I want activity B to react on the BACK-button. It should not let the user back to the starter splash screen but instead should send the app to the background (home button pressed).

But activity B never reacts on it. No logcat output is ever written.

This is my code of activity B.

public class StartScreenTab extends TabActivity {
    private final static String TAG = StartScreenTab.class.getSimpleName();
    private MyTabHostWrapper tabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTab();

    }

    private void initTab() {
        Log.i(TAG, "init Main Tabs...");
        tabHost = new MyTabHostWrapper(this);
        // tab Start
        tabHost.addNewTabSpec("tab-start", R.string.app_name,
                R.drawable.ic_tab_start, StartScreen.class);

        // tab Karte
        tabHost.addNewTabSpec("tab-karte", R.string.umkreis_activity_name,
                R.drawable.ic_tab_karte, UmkreisActivity.class);

        // tab Liste
        tabHost.addNewTabSpec("tab-liste", R.string.liste_activity_name,
                R.drawable.ic_tab_liste, ListeActivity.class);

        // tab Settings
        tabHost.addNewTabSpec("tab-prefs", R.string.menu_settings,
                R.drawable.ic_tab_preferences, MyPrefsActivity.class);

        tabHost.setFontColor("#FFFFFF");

    }

    @Override
    public void onDestroy(){
        Log.i(TAG, "onDestroy called.");
        super.onDestroy();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i(TAG, "KEY 'back' released");
            onBackPressed();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Log.i(TAG, "KEY 'back' pressed");
            onBackPressed();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Log.i(TAG, "BACK pressed");
        moveTaskToBack(true);
        return;
    }
}

I can see the logcat output from the init and from the onDestroy. But nothing from onKeyDown, onKeyUp and onBackPressed.

Can you help me please? Thx.

4

2 回答 2

1

原因是 TabActivity 失去焦点,我找到了解决方案,在你的正常 Activity 中调用代码

@Override
public void onBackPressed() {
    if (getParent() != null) {
        getParent().onBackPressed();
    } else {
        super.onBackPressed();
    }
}
于 2015-12-08T08:51:05.447 回答
0

我认为这可能是由tabHost引起的。我没有为绑定到选项卡的活动指定任何焦点命令,因此“返回”事件可能在某处丢失。

我通过逃避整个情况解决了我的问题。我删除了启动活动 (A) 并将其初始化函数移动到 StartScreenTab 活动 (B)。

所以我不需要对后面的事件做出任何进一步的反应,因为标准行为现在令人满意。

于 2013-10-30T20:22:21.910 回答