androidandroid-intentflagstabactivityactivitygroup

Android which Intent Flag should be used for ActivityGroup?


I have a TabActivity and the first tab is an ActivityGroup and I'm using the code below for this;

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);
    }
}

And in my ActivityGroup, I want to go another Activity after button click therefore I'm using the code below:

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);
            }
        });
    }
}

It works but there is a problem, when I click the first tab while in the FirstActivity, I can not go to MyActivityGroup. But for example, after clicking SecondTab, if I click first tab, I can go to MyActivityGroup.

To solve this problem, I think I should change the Intent flag, but I couldn't manage it. Please help me.


Solution

  • Can you show me some more? I had it implemented like this:

        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);
    

    Do you also want to see the activitygroup itself?

    Ok, here it is:

    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;
        }
    }