5

I am trying to make an application which have 4 tabs at the bottom of the screen.

All of them contain Activity (Intent).

And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.

Let me know as quickly as possible if you know about it.


Shaiful

Shaiful
  • 5,643
  • 5
  • 38
  • 41

3 Answers3

3

The problem of error occuring due to the replacement of activities can be solved 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);
Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
1

This is implemented in Tabs with multiple activities in a single tab.

However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem

Also need to Replace an activity in a tab, However from child activity. How is that to be done?

Vicky Kapadia
  • 6,025
  • 2
  • 24
  • 30
  • Without being sure, I guess the stack overflow happens aften having switched many times between tabs. This because the activities are managed using an activity stack (see http://developer.android.com/reference/android/app/Activity.html). It must be possible to replace the activity located at the top of the stack by the newly called one, instead of pushing it. But I'm not sure this is the best way to go (this is a real question). – Shlublu Jul 20 '11 at 21:31
1

At any one moment there may only be one activity. Docs about this here

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
sagus_helgy
  • 1,417
  • 1
  • 18
  • 30