androidback-buttonpressed

onBackPressed not called in TabActivity


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.


Solution

  • I think it may be caused by the tabHost. I didn't specify any focus command for the activities bound to the tabs, so maybe the "back" event is lost somewhere.

    I solved my problem by evading the whole situation. I removed the splash activity (A) and moved its init-functions to the StartScreenTab activity (B).

    So I don't need to react on the back-event any further because the standard behaviour is now satisfying.