Search code examples
androidandroid-activityandroid-tabhost

Android: Opening Activity with data with TabHost


My app is like this:

Tabhost:

  • MapViewActivity
  • ListViewActivity -> DetailActivity

In the DetailActivity there's a button to show something in the MapViewActivity. How do i close the DetailView and open the first tab with additional data, without loosing the tab hierarchy .

What i tried:

Intent intent = new Intent(this, TabsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.putExtra(DATA_KEY, "some-data");
startActivity(intent);

But in the onResume in TabsActivity is wasn't able to retrieve the data from

getIntent();

So another option would be to set a listener and before finishing the Activity, notify the listener. But here the problem is that PutExtra wont take a Class as a value.

I think i'm not on the right track. So what do you suggest?


Solution

  • I ended up using the following:

    In the ListViewActivity i use startActivityForResult(); like:

    Intent intent = new Intent(activity, DetailActivity.class);
    startActivityForResult(intent, REQUEST_CODE);
    

    When i click a button, the DetailActivity sends the data back to ListViewActivity

    Intent intent = new Intent();
    intent.putExtra(KEY_ID, id);
    setResult(RESULT_OK, intent);
    finish();
    

    Then i receive the data in ListViewActivity using the onActivityResult():

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == TrackDetailsActivity.REQUEST_CODE){
            String objectId = data.getStringExtra(DetailActivity.KEY_ID);
            if (objectId != null) {
                CustomTabActivity tabs = (CustomTabActivity)getParent();
                tabs.getTabHost().setCurrentTabByTag(MapViewActivity.TAG);
                tabs.setObject(objectId);
            }
        } 
    
        super.onActivityResult(requestCode, resultCode, data);
    }
    

    As you can see i store the result in my CustomTabActivity, later in MapViewActivity use this property.