Android Fragment vs. Activity Group
Asked Answered
S

1

7

I am working on an Android App where I want to implement an Activity Group for each tab. But since Activity Group is deprecated I have to use Fragments. I googled the last days and did some research on that topic but I still don't get it. The graphic below describes what I want to do. Also I'm coming straight from iOS and I need some feedback about my theories.

enter image description here

As you can see every Fragment consists of a WebView Fragment. When the user clicks on a link in that WebView Fragment - the request gets caught and a new Fragment is replaced which again holds a WebView and loads the link which was clicked in the previous Fragment. At a point the user decides to go back to the first Fragment and presses the back button.

The Fragments should pop off the stack in the reverse order until he again sees the first one. Ideally each Fragment should save his instancestate, so that when the user goes back the WebView doesn't need to load the site again.

I've come across ActionBar Sherlock which provides an example about Fragments Tabs. There is also an Example of an Fragment Stack. It would be ideal to combine these two examples, so that each tab consist of a Fragment Stack.

My code structs look like the following:

My TabHost Fragment

public class MyActivity extends SherlockFragmentActivity {
static TabHost mTabHost;
static TabManager mTabManager;

public static int THEME = R.style.Theme_Sherlock_Light;

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(THEME); //Used for theme switching in samples
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_tabs);
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    mTabManager = new TabManager(this, mTabHost, R.id.myRealTabContent);

    // adding some Fragment Tabs
    mTabManager.addTab(mTabHost.newTabSpec(tabNames[i]).setIndicator(tabNames[i]),
                FragmentStackSupport.CountingFragment.class, bundle);


    if (savedInstanceState != null) {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("tab", mTabHost.getCurrentTabTag());
}



// defining the Tab Manager

How would the Fragment Stack for each Tab should look like? Currently I have a Fragment Activity with a Fragment inside of each tab. But I don't achieve the logic I need.

Here is my code: My Fragment with WebView inside

I would be glad for some feedback and hints (or are there any examples on the web?). I also have some concerns about the memory - when the user clicks and clicks and clicks and the memory has to hold each Fragment in the stack.

Cheers.

Synchronism answered 22/10, 2012 at 17:58 Comment(2)
+1 for a well-explained question.Transeunt
did you ever find a solid solution to this issue?Eulalie
B
2

I can't think of many good reasons to create a new fragment each time the user clicks a link. Is there a reason that you're trying to do that? If you simply return false from shouldOverrideUrlLoading() instead of creating a new fragment, the WebView will load the new page, and if you want to go back, you can use the canGoBack() and goBack() methods. To handle back button presses, you can override onKeyDown in your activity, and do something like:

    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() {
        myWebView.goBack();
        return true;
    }
Banditry answered 22/10, 2012 at 19:59 Comment(3)
Thanks for your response. I agree that it is suboptimal but I have my reasons. Although I think I will follow your advice - its much more easier. Nevertheless I am interested in how one would implement a fragment stack for each tab.Synchronism
Well, since you asked, I'm sure it's not a great idea, but you could just literally have a Stack (or other data structure) of Fragments for each tab. Each time the user clicks a link, add the fragment to that tab's Stack (you wouldn't want to add any of your Fragments to the application's backstack for obvious reasons). Then, catch the back button press and see if the currently displayed tab's stack is non-empty and, if so pop the next one off to be displayed in the correct tab.Banditry
Maybe having a separate activity per tab, and then each activity would have it's own fragments?Bunco

© 2022 - 2024 — McMap. All rights reserved.