Restarting an activity in a single tab in a TabActivity?
Asked Answered
C

3

5

I have a TabActivity. Each tab point to a sub activity. This works great.

Is there any clever way to refresh one of the activity tabs? I just want to 'restart' the activity in tab #3 for example. Not sure of a good way to do this other than building in refresh support to the activity itself, or clearing ALL the tabs and recreating all of them.

Thanks,

Course answered 12/3, 2010 at 0:9 Comment(0)
C
6

Slightly more dynamic solution:

LocalActivityManager manager = getLocalActivityManager();
String currentTag = tabHost.getCurrentTabTag();
Class<? extends Activity> currentClass = manager.getCurrentActivity().getClass();
manager.destroyActivity(currentTag, true);
manager.startActivity(currentTag, new Intent(this, currentClass));
Cataclysmic answered 19/11, 2011 at 4:53 Comment(2)
LocalActivityManager is deprecated.Ingoing
Thanks Euthyphro. This was written a year and a half ago. Did someone already provide a better answer? If so, which one?Cataclysmic
P
4

I've not tried this myself, but typically you access each individual tab's Activity using the LocalActivityManager. This can be retrieved in a TabActivity by using getLocalActivityManager().

It looks like you should be able to use destroyActivity() and startActivity() to restart an Activity, though I'm not exactly sure if this will work (as I've not done it myself). One important thing to note is that the id of the Activity will be equivalent to the tag you set for the tab (e.g., the String you provided to TabHost.newTabSpec(String)).

LocalActivityManager manager = getLocalActivityManager();
manager.destroyActivity("tab3", true);
manager.startActivity("tab3", new Intent(this, ThirdTab.class));
Psychopath answered 12/3, 2010 at 1:17 Comment(1)
This generates a NullPointerException for me at android.widget.TabHost.setCurrentTab() line 328.Rhizopod
E
0

Here is the solution:

tabHost.setOnTabChangedListener(this);
public void onTabChanged(String tabId) {
        Log.d(LOG_KEY, tabId);
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity("ID_1", true);
        manager.startActivity("ID_1", new Intent(this, YourMyActivity.class));
    }
Endomorphic answered 10/3, 2011 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.