问题描述
- 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。通过检测改变来开启。
这是错误的方法。更改事件总是会发生在活动启动后,你应该做的逻辑也不同。
解决方案二:
不需要使用 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)) {...
时间: 2024-11-08 23:18:49