【问题标题】:Android which Intent Flag should be used for ActivityGroup?Android ActivityGroup 应该使用哪个 Intent Flag?
【发布时间】:2012-01-07 12:04:33
【问题描述】:

我有一个TabActivity,第一个标签是ActivityGroup,我为此使用下面的代码;

public class MyTabActivity extends TabActivity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();
        TabHost.TabSpec tabSpec;
        Intent intent;
        Resources resources = getResources();

        intent = new Intent(MyTabActivity.this, MyActivityGroup.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tabSpec = tabHost.newTabSpec("tab1");
        tabSpec.setIndicator("Tab1", resources.getDrawable(R.drawable.ic_launcher));
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);

        intent = new Intent(MyTabActivity.this, SecondTab.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tabSpec = tabHost.newTabSpec("tab2");
        tabSpec.setIndicator("Tab2", resources.getDrawable(R.drawable.ic_launcher));
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);

        tabHost.setCurrentTab(0);
    }
}

在我的ActivityGroup 中,我想在button 之后点击另一个Activity,因此我使用下面的代码:

public class MyActivityGroup extends ActivityGroup
{
    private Button button;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener()
        {           
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MyActivityGroup.this, FirstActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                View view = getLocalActivityManager().startActivity("firstActivity", intent).getDecorView();
                setContentView(view);
            }
        });
    }
}

它可以工作,但是有一个问题,当我在FirstActivity 中单击第一个选项卡时,我无法转到MyActivityGroup。但是例如点击SecondTab后,如果我点击第一个标签,我可以转到MyActivityGroup

为了解决这个问题,我认为我应该更改 Intent 标志,但我无法管理它。请帮帮我。

【问题讨论】:

  • 就像我说的,如果您在第一个选项卡中并且更深一层,则选项卡主机不会刷新活动组并转到组的根目录。你必须按照我说的做,使用 onclicklistener。

标签: android android-intent flags tabactivity activitygroup


【解决方案1】:

你能再给我看一些吗?我是这样实现的:

    Intent i = new Intent(getParent(), SomeClass.class);

    View view = SomeActivityGroup.group.getLocalActivityManager()  
    .startActivity("SomeClass", i  
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
    .getDecorView();  

    // Again, replace the view  
    SomeActivityGroup.group.replaceView(view);

您还想查看活动组本身吗?

好的,就是这样:

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class SomeActivityGroup extends ActivityGroup {

    View rootView;

    // Keep this in a static variable to make it accessible for all the nested
    // activities, lets them manipulate the view
    public static SomeActivityGroup group;

    // Need to keep track of the history if you want the back-button to work
    // properly, don't use this if your activities requires a lot of memory.
    private ArrayList<View> history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.history = new ArrayList<View>();
        group = this;

        // Start the root activity within the group and get its view
        View view = getLocalActivityManager().startActivity("SomeActivity", new Intent(this, RootActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

        // Replace the view of this ActivityGroup
        replaceView(view);
    }

    public void replaceView(View v) {
        // Adds the old one to history
        if (history.size() == 0) {
            if (rootView != null) {
                history.add(rootView);
                rootView = null;
            }
        }
        history.add(v);
        // Changes this Groups View to the new View.
        setContentView(v);
    }

    public void back() {
        try {
            if (history.size() > 0) {
                if (history.size() == 1) {
                    rootView = history.get(0);
                }
                history.remove(history.size() - 1);
                setContentView(history.get(history.size() - 1));
            } else {
                finish();
            }
        } catch (Exception ex) {
        }
    }

    @Override
    public void onBackPressed() {
        try {
            SomeActivityGroup.group.back();
        } catch (Exception ex) {
        }
        return;
    }
}

【讨论】:

  • 我添加了 ActivityGroup。我想提一下 ActivityGroup 在 ICS 中已被弃用。最好使用 Fragments。
  • 感谢您的回复。据我了解,您的代码提到了 ActivityGroup。但是我也有一个问题,由于您的代码,当我在 RootActivity 并单击选项卡时,我无法转到 SomeActivityGroup。我该如何解决这个问题?
  • 为什么不呢?我从 RootActivity 转到 ActivityGroup,这在我的答案的第一个代码示例中显示。
  • 好的,我无法准确解释我的问题。在单击按钮后的 MyActivityGroup 中,我将转到 FirstActivity。我正在使用我的问题中的代码。然后,当我在 FirstActivity 中时,单击选项卡不会移动到 MyActivityGroup。但是当我在 FirstActivity 中时,单击另一个选项卡正在工作,并且在另一个选项卡之后,当我单击第一个选项卡时,我可以转到 MyActivityGroup。为什么我不能直接从 FirstActivity 转到 MyActivityGroup?
  • 如果我理解正确的话,那就完全不同了。您想从选项卡中转到 RootActivity。检查我的其他答案。
【解决方案2】:

如果是第一个选项卡,那么应该在

中设置
public class Tabhost extends TabActivity {

..

    getTabWidget().getChildAt(0).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (tabHost.getCurrentTab() == 0) {
                // tabHost.setCurrentTab(1); // UGLY!!
                // tabHost.setCurrentTab(0); // Had this before, but replaced it with :
                SomeActivityGroup.group.onResume();
            } else {
                tabHost.setCurrentTab(0);
            }
        }
    }

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多