0

Here's my layout design:

    MainActivity
         |
    MainFragment
         |
      TabHost
         |
_____________________
|     |     |     |
Tab1  Tab2  Tab3  Tab4
      |
     ft.replace(MainActivityContainer, FragmentThatDoesSomething)
      |
   FragmentThatDoesSomething

As you can see, there's a button that does a ft replace on the main activity container and loads FragmentThatDoesSomething to accomplish a task.

In FragmentThatDoesSomething I want to have a button that goes back to Tab2 of the TabHost and keep the tabs.

I use the activity to transfer data between fragments so in the activity is where I would like to perform some ft transaction to get back to tab2.

What's the easiest way to do this? If I run an ft transaction with the getChildFragmentManager and Tab2's container I run into this problem

because I'm using the support v4 Fragment class.

Community
  • 1
  • 1
thecodingmate
  • 331
  • 6
  • 16
  • Use a `ViewPager` and the new `TabLayout`. It is easy to use and implement. – Jared Burrows Aug 05 '15 at 21:22
  • It looks like TabLayout is API 21 which would cut out a lot of phones. Looks like something I'll have to do eventually though. Hmm – thecodingmate Aug 06 '15 at 02:02
  • TabLayout is not API 21. Use the new Design Support library. – Jared Burrows Aug 06 '15 at 02:07
  • Ok I implemented TabLayout and ViewPager. However now when I try to launch FragmentThatDoesSomething from Tab 2 the fragment overlays against Tab 2's fragment. I can still see the button from Tab 2 and the background. I'm trying to launch FragmentThatDoesSomething to replace Tab 2. – thecodingmate Aug 06 '15 at 18:47
  • Please just use this example: https://github.com/chrisbanes/cheesesquare. Everything is done for you. – Jared Burrows Aug 06 '15 at 18:52
  • 1
    Thanks for the example. I corrected my mistakes and chose a different path instead of nested fragments. – thecodingmate Aug 07 '15 at 02:24

3 Answers3

0

I would use addToBackStack(null) for my fragment transactions. Then the button defined in FragmentThatDoesSomething could call getActivity.onBackPressed().

At that point it is just a matter of making sure to save state.

// MainActivity.java
@Override
public void someCallbackMethod(Object obj1, Object obj2) {
    // have MainActivity store your values and Tab2 fragment accesses data
    // through callback references when Tab2.onCreateView() is called.

    onBackPressed();

    // OR retrieve your Tab2 fragment and manually update values here via 
    // some kind of accessor
}
zec
  • 294
  • 1
  • 9
  • How would I pass data from FragmentThatDoesSomething to Tab2? Normally I would use a callback, pass it back to the activity, attach it to a bundle, and ft transact with the fragment or activity. – thecodingmate Aug 06 '15 at 00:30
  • Similar approach - have your button event method reference the callback method passing the data along. – zec Aug 06 '15 at 12:31
0

I chose to implement activities as handling nested fragments are a pain.

One gotcha I ran into was sending data back from FragmentThatDoesSomething (now an activity) back to Tab 2. Because Tab 2 is a nested fragment of MainFragment, I had to override onActivityResult in MainFragment to send data back to the child Tab 2. I think its because I'm using the v4 Fragment support class.

I did that using this code.

thecodingmate
  • 331
  • 6
  • 16
0

compile 'com.android.support:design:22.2.0' I thing you need to use above material design library to solve your issue . you can add multiple fragment in one viewPager .

It will provide you more other option to do .