Search code examples
androidandroid-widgetandroid-tabhostandroid-tabactivity

Moving to previous activity in activity group in android


I'm developing an android application which contains a tabhost having 4 tabs. All 4 tabs navigate to same class Activity1 which extends activitygroup.

From Activity1 i'm again navigating to another activity Activity2 using following code,

Intent intent = new Intent(arg1.getContext(), Activity2.class);
            replaceContentView("sample", intent, Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

Now from this new activity i need to come back to Activity1 when device back button is pressed. I overrided onbackpressed() method in Activity2 with following code,

Intent intent = new Intent(this, Activity1.class); 
    Activity parentActivity = (Activity1)getParent(); 
    parentActivity.replaceContentView("sample", intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );

When i run the code, it actually move out of Activity2 but it also move out of Activity1. what i need is to come back to Activity1 from Activity2...can anyone help me?


Solution

  • A simple call to finish() can address your needs.

    You can refer Android: Go back to previous activity for detailed description.