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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With