繁体   English   中英

为什么我的Android标签无法正常工作?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM