程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> activity-Android 的 Tab 不能正常運行

activity-Android 的 Tab 不能正常運行

編輯:編程綜合問答
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();
        }
    }

最佳回答:


你是想在 intent 上輸出:b.putBoolean("isTrendingTab",true); 或者 false。通過檢測改變來開啟。
這是錯誤的方法。更改事件總是會發生在活動啟動後,你應該做的邏輯也不同。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved