Question

I'm implementing an App with a Tabhost. In one of my tabs i'm executing more than one activities (ActivityGroup).

For example: I'm in activity one, after clicking in a listview i go to activity two in the same tab. The problem is when i press back button, the application closes and i want to go back to activity one. I've overwritten onBackPressed() method, onKeyDown method and they won't be called, they are not being fired. I've seen a lot of posts with the same problem but any of the solutions shown wont work.

would be glad for your help!

Creating tabs:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_tab);

    TabHost fichas = getTabHost();
    fichas.setup();     

    TabHost.TabSpec spec=fichas.newTabSpec("tab1");
    Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
    spec.setContent(i);
    spec.setIndicator("Hoy");
    fichas.addTab(spec);

    TabHost.TabSpec spec2=fichas.newTabSpec("tab2");
    Intent i2 = new Intent(getApplicationContext(), QueActivity.class);
    spec2.setContent(i2);
    spec2.setIndicator("Que");
    fichas.addTab(spec2);

    spec=fichas.newTabSpec("tab3");
    spec.setContent(R.id.Cuando);
    spec.setIndicator("Cuando");
    fichas.addTab(spec);

    spec=fichas.newTabSpec("tab4");
    spec.setContent(R.id.Donde);
    spec.setIndicator("Donde");
    fichas.addTab(spec);

}

On QueActivity.class i'm implementing onClickListener to show the new activity in the same tab:

public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            //creo el nuevo objeto para poder llamarlo

            Intent intent = new Intent(QueActivity.this, ProductosCategorias.class);

            //Creo la informacion para pasar entre actividades
            Bundle informacion= new Bundle();
            intent.putExtra("eventos",categorias.get(position).getEventos());
            Toast.makeText(getApplicationContext(), "has seleccionado "+ categorias.get(position).getNombre(), Toast.LENGTH_SHORT).show();
            //New activity
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            View view = (getLocalActivityManager().startActivity("productos", intent)).getDecorView();
            setContentView(view);                                   
        }

Activity requested. Here is where i want to override the onBackPressed()

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_productos_categorias);
    //Do stuff here 
    });

Outside onCreate method i'm overriding:

public void onBackPressed(){
    System.out.println("Fires");
    //Go back to the other intent
}

The problem is when i press the back key i don't get my overrided method fired, insted of that the application gets closed

Was it helpful?

Solution

This is because in ActivityGroup the onActivityResult event get called on your ActivityGroup class and not in the activity you are calling it.

This stack overflow question can solve your problem:

Issue with onActivityResult in tab activity

OTHER TIPS

Your main activity hosting tabwidget is getting the onBackPressed call try using callback interfaces or check if your current activity is in foreground at the time of back button press.

First, where u implemented onBackPressed, In your tabhost activity (which has all the tabs activity) or your tabs activity (QueActivity).

TabHost Activity (ActivityGroup) does not call onBackPressed on child tab activities. I think it has to be done manually.

TabHost Activity (ActivityGroup)

@Override
public void onBackPressed(){
    Activity currentActivity = getCurrentActivity();
    if(currentActivity != null)
        currentActivity.onBackPressed();
}

With the above code your QueActivity should get onBackPressed called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top