Aju
Aju

Reputation: 4599

How to switch from child activity to home activity in tabhost

I am implementing tab in my App. For that I made a class to handle the activities inside the tab. My problem is I started some activities one after another like A, B, C, D. Now I want to comeback to Activity A from D, and other activites like B, C and D should be destroyed. Please help me to implement this. Here is the code I am using to handle activities...

public class TabGroupActivity extends ActivityGroup {

protected Context mCTX;
protected ArrayList<String> mIdList;

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

    if(mIdList == null){
        mIdList = new ArrayList<String>();
    }

    //set context
    mCTX = this;

}

public void startChildActivity(String Id, Intent intent){
    Id+= System.currentTimeMillis();
    Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));

    if (window != null) {
        mIdList.add(Id);
        setContentView(window.getDecorView());
    }
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub

    int length = mIdList.size();
    if(length > 0){
        Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
        current.finish();
    }

}

@Override
public void finishFromChild(Activity child) {

    LocalActivityManager manager = getLocalActivityManager();
    int index = mIdList.size()-1;

    if (index < 1) {
        finish();
        return;
    }

    manager.destroyActivity(mIdList.get(index), true);
    mIdList.remove(index);
    index--;
    String lastId = mIdList.get(index);
    Intent lastIntent = manager.getActivity(lastId).getIntent();
    Window newWindow = manager.startActivity(lastId, lastIntent);
    setContentView(newWindow.getDecorView());
}


}

When I Start activity I will use the following code.

Intent locIntent = new Intent(HomeActivity.this,SomeActivity.class);
TabGroupActivity parent = (TabGroupActivity) getParent();
parent.startChildActivity("some id", locIntent);

One more doubt I have...How to switch an activity in one tab to an activity in another tab. Thanks in advance

Upvotes: 0

Views: 1673

Answers (3)

Shachilies
Shachilies

Reputation: 1616

Why dont you set the flag FLAG_ACTIVITY_CLEAR_TOP for new intent you are calling to go back to an Activity which was started earlier .

you need to set your launchMode to multiple instance in manifest file for that Activity, and use only FLAG_ACTIVITY_CLEAR_TOP.

As stated the theory of FLAG_ACTIVITY_CLEAR_TOP.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

I have used it and it works perfectly fine , So Please let me know if it makes sense to you or if you face any problem in implementing it.

Upvotes: 1

I&#241;igo
I&#241;igo

Reputation: 12823

Don't use Activities inside a TabHost, its a waste of memory. I'd rather use fragments instead. You can easily add them to the TabHost. This is a good tutorial:

http://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/

Upvotes: 5

Related Questions