Chatar Veer Suthar
Chatar Veer Suthar

Reputation: 15639

Another activity within TabHost?

I have made a tab host and showing 4 tabs in them, as below the code is

    /** Called when the activity is first created. */
      @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    TabHost tabHost = getTabHost();

    tabHost.addTab(tabHost.newTabSpec("list").setIndicator("List").setContent(
            new Intent(this, List.class)));
    tabHost.addTab(tabHost.newTabSpec("profile").setIndicator("Profile").setContent(
            new Intent(this, Profile.class)));
    tabHost.addTab(tabHost.newTabSpec("criteria").setIndicator("Criteria").setContent(
            new Intent(this, Criteria.class)));
    tabHost.addTab(tabHost.newTabSpec("more").setIndicator("More").setContent(
            new Intent(this, More.class)));
    }
 }

In Profile, I have a button which will edit profile of user, as I click the button, it calls method as below

    @Override
public void onClick(View v) {
System.out.println("Veer");
    Intent intent = new Intent().setClass(this, EditProfile.class);
Intent intent = new Intent(this, EditProfile.class);
}

Where is shows EditProfile, but the tabs are not visible as in iPhone it is visible all the time, now what should I do so that my tabs should be above and EditProfile be within Profile tab?

Upvotes: 2

Views: 2834

Answers (2)

Sandeep Kumar P K
Sandeep Kumar P K

Reputation: 7472

make your Profile activity extends ActivityGroup
then add following code to onClick(View v)

View view = getLocalActivityManager().startActivity("EditProfile", 
        new Intent(v.getContext(),EditProfile.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
        .getDecorView();
setContentView(view);

Upvotes: 1

Vicky Kapadia
Vicky Kapadia

Reputation: 6083

The Activities in the tab can be switched in the following manner.

First Let us understand the flow:

  1. We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now

  2. When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)

The code can be as follows..

  1. For going from list to details...

(This can be in the onClickListener)

private OnClickListener textListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Constants.SHOW_DETAILS = true;
        Intent intent = new Intent(context, DetailsActivity.class);
        replaceContentView("activity3", intent);
        }
};

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

}
  1. When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen

    @Override
      public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    if (MathHelper.SHOW_DETAILS) {
        Log.e("back", "pressed accepted");
        Constants.LIST_ACTIVITY = 1;
        Constants.SHOW_DETAILS = false;
        Intent intent = new Intent(this, Tab_widget.class);
        startActivity(intent);
        finish();
      }
     }
    

The most important part here is Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc

Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs

tabHost.setCurrentTab(Constants.LIST_ACTIVITY);

Upvotes: 1

Related Questions