Android Fragment vs. Activity group

Hi guys, I am working on an Android application where I want to implement a group of actions for each tab. But since the activity group is out of date, I have to use fragments. In the past days, I searched Google and did some research on this topic, but I still don't get it. The following figure shows what I want to do. I also go straight from iOS, and I need feedback on my theories.

enter image description here

As you can see, each fragment consists of a WebView fragment. When the user clicks on the link in this WebView fragment, the request falls in and replaces a new fragment, which again contains the WebView and loads the link that was clicked in the previous fragment. At some point, the user decides to return to the first fragment and presses the "Back" button.

Fragments should push the stack in the reverse order until it sees the first again. Ideally, each fragment should preserve its instancestate, so when the user returns, WebView does not need to load the site again.

I came across an ActionBar Sherlock, which provides an example of fragment tabs. There is also an example fragment fragment. It would be ideal to combine these two examples, so that each tab consists of a Fragment stack.

My code structures are as follows:

My TabHost snippet

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 

What will the stack of fragments for each tab look like? I currently have a Fragment Activity with a fragment inside each tab. But I do not reach the logic that I need.

Here is my code: My snippet with WebView inside

I would be happy for some feedback and tips (or are there any examples on the Internet?). I also have some memory issues - when the user clicks and clicks and clicks, and the memory must hold each fragment on the stack.

Greetings.

+7
source share
1 answer

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

  if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() { myWebView.goBack(); return true; } 
+1
source