English 中文(简体)
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; 
  } 
  } 
 }
问题回答

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));
    }

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".

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.

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.

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.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签