Nirav Bhandari
Nirav Bhandari

Reputation: 4610

how to return back to parent activity while pressing back button on chlild activity in TAB Application

i have 4 tab with activity group..all tab contain list of item and on press of any item its discriptioo will be displayed in new activity.. i m using activitygroup to embedded child activity in tab.and i m using replace contentview to change the activitygroup view.

when i press back button i call finish() from child and i immediately get out of application..is there any way to return back to parent activity using activity group...???

i m using following code to chang activitygroup view..bt dont know how to come back to this activity..

public void replaceContentView(String id, Intent newIntent) 
{
    View mview = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)).getDecorView(); 
    this.setContentView(mview);

}  

Upvotes: 0

Views: 3110

Answers (1)

Rahul Sharma
Rahul Sharma

Reputation: 3677

I was also stuck with this problem but solved it have a look a t below code hope will help you also

Your activityGroup should be something like this

public class ABCGroup extends ActivityGroup{

public static ABCGroup group;
private ArrayList<View> history;

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

    View view = getLocalActivityManager().startActivity
                ("ParentActivity", 
                new Intent(this, ParentActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();

    replaceView(view);
}

public void replaceView(View v) {
    // Adds the old one to history
    history.add(v);
    // Changes this Groups View to the new View.
    setContentView(v);
}

public void back() {  
    if(history.size() > 0) {  
        history.remove(history.size()-1);
        if(history.size()<=0){
            finish();
        }else{
            setContentView(history.get(history.size()-1));
        }
    }else {  
        finish();  
    }  
}

@Override  
public void onBackPressed() {  
    ABCGroup.group.back();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK){
        ABCGroup.group.back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
}

In your parent activity

View mview = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)).getDecorView();
ABCGroup.group.replaceView(v);

In your child activity use

public boolean onKeyDown(int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK){
        ABCGroup.group.back();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 2

Related Questions