[英]Why aren't my Android Tab's Working Properly?
我有一個TabActivity,其中有一個帶有兩個標簽的TabHost。 每個選項卡都有其自己的意圖。 在我可以檢測到選項卡是否更改之前,似乎觸發了intent的onResume()。 我該如何解決?
TabActivity代碼:
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();
}
}
}
}
這是我的一項活動的意圖中的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();
}
}
如果我正確理解問題是您嘗試放
b.putBoolean("isTrendingTab",true);
(或錯誤)通過檢測更改來確定要啟動的意圖。
那是錯誤的方法。
更改事件將始終在活動啟動后發生,您應該執行不同的邏輯。 您必須重新考慮它。
您是否看過活動的生命周期? 當活動也被創建時,將調用履歷,並且bundle.getBoolean(“ isTrendingTab”)行沒有默認值,以防尚未設置。
您可以先在onCreate中將其設置為默認值嗎? 我認為那是你的問題。 代碼有點草率。 您正在嘗試將變量傳遞給每個活動,但是變量仍然在選項卡活動中同時存在。 視圖將是更好的方法,因此它們都可以在選項卡活動中看到相同的變量。
將在選項卡宿主的onresume方法之前調用類ARActivity.class的oncreate。
因此,您可以在ARActivity中進行所需的任何處理。
同樣由於您的tabHost.setCurrentTab(0); 您的開始標簽將始終是ARActivity。
並且,如果要根據選項卡更改來激活代碼,請使用主選項卡宿主ontabchange來確定要使用的選項卡,並使用ID,然后向內部廣播接收器發送請求。
if (tabHost.getCurrentTab() == 0) {
i.setAction(getString(R.string.br_refresh_home_tab));
sendBroadcast(i);
} else {
i.setAction(getString(R.string.br_refresh_sports_tab));
sendBroadcast(i);
}
在您的ARActivity中,
protected class RefreshList extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(
getString(R.string.br_refresh_home_tab))) {
}
}
}
無需使用onTabChanged()。
這是我在應用程序中執行的操作(粘貼了您的一些值)。 我將boolean標志添加到意圖中,而不是額外的包:
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)) {...
我發現當對多個選項卡使用相同的類時,如上所述,我不得不在每個Intent構造函數中使用不同的操作字符串來區分它們。 否則,在同一類的選項卡之間切換時,它不會創建新的活動。 您似乎尚未這樣做(因此),因此您可以繼續省略它。 我以為我會提到它,因為通過isTrendingTab建議您可能會沿着這條路線前進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.