D..
D..

Reputation: 71

Android: How to restart an activity within a tabhost?

I've searched and I know it seems some people frown upon using activities within tabs, but moving past that...how would I restart a tabbed activity while still keeping the tabs visible? I have an activity in a tab, I use the menu to create a new activity to update the tab's activity displayed info, when I return from the menu activity I want the new information to be displayed in the tab's activity. I am using startActivityForResult() from the menu choice, but when I return and try to restart the activity...it wipes out the tabs above(I guess as expected, but I want to re-launch the refreshed activity within the tab).

Creating the tabs:

  TabHost host = getTabHost();
  Intent home_intent = new Intent(constants.HOME_ACTION,
    null, this, homeTab.class);
  Intent inbox_intent = new Intent(constants.INBOX_ACTION,
    null, this, inboxTab.class);
  Intent stats_intent = new Intent(constants.STATS_ACTION, null,
    this, infoTab.class);

  host.addTab(host.newTabSpec(constants.HOME_TAG)
    .setIndicator(getText(R.string.home_label),
      getResources().getDrawable(R.drawable.icon))
    .setContent(home_intent));
  host.addTab(host.newTabSpec(constants.INBOX_TAG)
    .setIndicator(getText(R.string.inbox_label),
      getResources().getDrawable(R.drawable.icon))
    .setContent(inbox_intent));
  host.addTab(host.newTabSpec(constants.STATS_TAG)
    .setIndicator(getText(R.string.stats_label),
      getResources().getDrawable(R.drawable.icon)).setContent(
      stats_intent));

Return from the menu activity in the tab's activity(updating database info):

  public void onActivityResult(int requestCode, int resultCode, Intent data) {     
  super.onActivityResult(requestCode, resultCode, data); 
  switch(requestCode) { 
  case (constants.ACTIVITY_REQUEST_CODE_UPDATE_PROFILE) : { 
   if (resultCode == Activity.RESULT_OK) { 
    boolean profileUpdated = data.getBooleanExtra(constants.ACTIVITY_BUNDLE_UPDATE_PROFILE, false);
    Log.d(LOG_TAG, "activity returned with " + profileUpdated);
    // Check to see if we updated our profile to refresh the screen
    if(profileUpdated == true){
     // Refresh the screen with the new info
     homeTab.this.finish();
     this.startActivity(getIntent());
    }
   } 
   break; 
  } 
  } 
 }

Upvotes: 0

Views: 9932

Answers (5)

Pushan
Pushan

Reputation: 480

Here is the SOLUTION:

tabHost.setOnTabChangedListener(this);
public void onTabChanged(String tabId) {
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity("ID_1", true);
        manager.startActivity("ID_1", new Intent(this, YourMyActivity.class));
    }

Upvotes: 1

Cavina
Cavina

Reputation: 43

I suggest you to do something like this (NOT EXTENDING TABACTIVITY):

mlam = new LocalActivityManager(this, false);
final TabHost tabHost = (TabHost) findViewById(R.id.tabhostfaces);
mlam.dispatchCreate(bundle);
mlam.dispatchResume();
mlam.dispatchPause(isFinishing());
tabHost.setup(mlam);

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
    @Override
    public void onTabChanged(String tabId) {
        String currentTag = mTabHost.getCurrentTabTag();
        if(currentTag.equals("tab_ntflist")){
            ntfreglst.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mlam.startActivity(currentTag, new Intent(ntfreglst));
        } else if(currentTag.equals("tab_profile")){
            pflvw.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            mlam.startActivity(currentTag, new Intent(pflvw));
        }
    }
});

Where ntfreglst and pflvw are Intents already defined. The flag indicates that, when you call startActivity, as the activity is already running, only the onResume method will be invoked This way, everytime the tab changes, the ONRESUME method will be invoked. This way you can make all your updates inside the onResume method.

Upvotes: 0

Androider
Androider

Reputation: 21335

Well ... I don't think the activities are necessarily a good idea either. Please note however that any activity can register a broadcast receiver, and any activity can send broadcasts .... Perhaps you could register a broadcast receiver and communicate that way instead.

Upvotes: 0

Karl Rosaen
Karl Rosaen

Reputation: 4644

Yeah, I think it's safe to say that finishing and restarting yourself as an Activity within a tab is not a supported use case. Instead, when you know a "profile has been updated", is there a better finer grained way to refresh its state? E.g query a content provider to refresh the information represented in the activity? It all depends on what information is being represented in the Activity.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

some people frown upon using activities within tabs

Hi! I'm "some people"!

how would I restart a tabbed activity while still keeping the tabs visible?

You don't, AFAIK.

Of course, since you're the one who is finish()-ing and restarting the activity, you can easily stop that from happening by commenting out those two lines of code. Your problem isn't that you're losing the tabs -- it's that you're smashing your activity with a sledgehammer when there is probably a better way of doing whatever sort of "refresh" you are trying to achieve.

Of course, doing that sort of refresh would probably be easier if you had Views for your tabs instead of Activities, which is one of the reasons "some people frown upon using activities within tabs".

Upvotes: 0

Related Questions