1

In my app, I have one tab which includes two activities say 1 and 2. Activity 1 extends ActivityGroup, which has one child activity say A. Now scenario becomes like this, In first tab --> Activity 1 and its child activity A. In second tab --> Activity 2. After doing some functionality in Activity 1 of first tab, user moves to child activity A of first tab. Now what I want to do, when user presses back button from child activity A, user should move back to its parent activity 1. How to do that? Below is my code..

MainActivity.java

public class MainActivity extends TabActivity {

    TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Resources ressources = getResources(); 
        tabHost = getTabHost(); 

        Intent intentProfile = new Intent().setClass(this, Tab1.class);
        TabSpec tabProfile = tabHost
          .newTabSpec("Profile")
          .setIndicator("Profile", ressources.getDrawable(R.drawable.ic_launcher))
          .setContent(intentProfile);

        Intent intentFriends = new Intent().setClass(this, Tab2.class);
        TabSpec tabFriends = tabHost
          .newTabSpec("Friends")
          .setIndicator("Friends", ressources.getDrawable(R.drawable.ic_launcher))
          .setContent(intentFriends);


        tabHost.addTab(tabProfile);
        tabHost.addTab(tabFriends);

        tabHost.setCurrentTab(0);

    }


    public void switchTabBar(int tab) {
        tabHost.setCurrentTab(tab); 
    }
    @Override
      public void onBackPressed() {
        // Called by children
        MainActivity.this.finish();
        }

}

Tab1.java

public class Tab1 extends ActivityGroup {

    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab1);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent in = new Intent(Tab1.this, Second.class);
                replaceContentView("activity3", in);
            }
        });
    }

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

     @Override
        public void onBackPressed() {
          this.getParent().onBackPressed();   
        }

}

Second.java

 public class Second extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
        }

        @Override
        public void onBackPressed() {
          this.getParent().onBackPressed();   
          MainActivity parentTab = (MainActivity) this.getParent();
          parentTab.switchTabBar(0);
        }
    }

Tab2.java

public class Tab2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);
    }


    @Override
    public void onBackPressed() {
    // this.getParent().onBackPressed();
         MainActivity parentTab = (MainActivity) this.getParent();
         parentTab.switchTabBar(0);

    }
}

Here I can move to tab 1 from tab 2 on back button pressed. But, I can't move back to parent activity from child activity of tab 1.

Looking Forward
  • 3,579
  • 8
  • 45
  • 65

2 Answers2

0

You have to override back button in parent activity and make a call from child. For more info please check this link : Navigate Between Activities in an ActivityGroup

Community
  • 1
  • 1
Harish Godara
  • 2,388
  • 1
  • 14
  • 28
0

In Tab1:

    @Override
    public void onBackPressed() {
      super.onBackPressed();   
    }

    @Override
    public void onPause() 
    {
      super.onPause();
       finish();
    }

in Tab1

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67