2 znl 12 znl_12 于 2013.03.04 16:15 提问

Android 的 Tab 不能正常运行

在 TabActivity 有一个 TabHost,在 TabHost 中有两个选项卡,每个选项卡都有自己的intent。如果一个 tab 改变,在我检测之前,这个 intent 的 onResume()方法被销毁。如何解决这个问题呢?
TabActivity code:

public class TabHostActivity extends TabActivity {
    static final int SHOW_SHARE_ACTIVITY = 0;
    static final int SHOW_LOGIN_ACTIVITY = 1;

    private TabHost tabHost;
    private ImageButton composeImageButton;
    private SharedPreferences prefs;
    private Bundle b;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tabhostactivity);       
        prefs = getSharedPreferences(Constants.PREFS_NAME, 0);
        //Setup the ActionBar
        composeImageButton = (ImageButton) findViewById(R.id.composeImageButton);
        composeImageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(prefs.getBoolean("isLoggedIn", false))
                {
                    showShareActivity();
                }
                else
                {
                    Intent intent = new Intent(TabHostActivity.this, LoginActivity.class);
                    startActivityForResult(intent, SHOW_LOGIN_ACTIVITY);
                }
            }
        });

        b = new Bundle();
        //Setup the Tabs
        Resources res = getResources(); // Resource object to get Drawables
        tabHost = getTabHost();  // The activity TabHost
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
               @Override
              public void onTabChanged(String arg0) {
                   if(tabHost.getCurrentTab() == 0) //Check if the Watchlist tab was clicked so we can prompt login
                   {
                        //Toast toast = Toast.makeText(getApplicationContext(), "TRENDING = YES", Toast.LENGTH_SHORT);
                        //toast.show();
                        b.putBoolean("isTrendingTab",true);
                   }
                   else
                   {
                       Toast toast = Toast.makeText(getApplicationContext(), "TRENDING = NO", Toast.LENGTH_SHORT);
                       toast.show();
                       b.putBoolean("isTrendingTab",false);
                   }
              }     
        });  

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ARActivity.class);
        intent.putExtras(b);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, WatchlistActivity.class);
        intent.putExtras(b);
        spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }

    private void showShareActivity()
    {
        Intent intent = new Intent(TabHostActivity.this, ShareActivity.class);
        startActivityForResult(intent, SHOW_SHARE_ACTIVITY);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == SHOW_LOGIN_ACTIVITY)
        {
            //Login was successful so lets show the compose box!
            if (resultCode == RESULT_OK) {
                showShareActivity();
            }
        }
    }
}

这是其中一个 activity 中intent里的 onResume 方法:

public void onResume()
    {
        super.onResume();

        Bundle bundle = getIntent().getExtras();

        if(bundle.getBoolean("isTrendingTab"))
        {
            Toast toast = Toast.makeText(getApplicationContext(), "TRENDING!", Toast.LENGTH_SHORT);
            toast.show();
        }
        else
        {
            Toast toast = Toast.makeText(getApplicationContext(), "WATCHLIST!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }

2个回答

myc_100
myc_100   2013.03.04 17:55
已采纳

你是想在 intent 上输出:b.putBoolean("isTrendingTab",true); 或者 false。通过检测改变来开启。
这是错误的方法。更改事件总是会发生在活动启动后,你应该做的逻辑也不同。

O_Casper_ne
O_Casper_ne   2013.03.07 11:34

不需要使用 onTabChanged()

Intent intent = new Intent(action) // see notes below about "action"
    .setClass(this, ARActivity.class)
    .putExtra("isTrendingTab", true); 
TabHost.TabSpec spec = tabHost.newTabSpec("trending")
    .setIndicator("trending", getResources().getDrawable(drawableId))
    .setContent(intent);
tabHost.addTab(spec);

在 onResume()方法中:

if (getIntent().getBooleanExtra("isTrendingTab", false)) {...
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!