Using viewer page to switch between two fragments - however it disappears after switching into a fragment

dark_illusion_909099

I have a problem with my viewer page, so in this java file that I have which extends from Fragment has some code to do with view pager. Basically in my screen I want to let the user to swipe cross to the right and swipe across to the left. It works fine as it should be however I have other fragments too within the project I am working on so if I go to a different fragment and come back to this screen which lets me do this swipe feature, all for a sudden everything disappears from the screen. Any idea how to fix this issue ??

This is the java file;

public class ExampleFragment extends Fragment {
   View v;
   ViewPager viewPager;
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

      final View v =  inflater.inflate(R.layout.tab2_main, container, false);

      viewPager = (ViewPager)v.findViewById(R.id.accountswitching);
      AccountSwitching accountSwitching = new AccountSwitching(getFragmentManager());
      viewPager.setAdapter(accountSwitching);

This is the code I have inside the AccountSwitching java class;

public class AccountSwitching extends FragmentPagerAdapter {
public AccountSwitching(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {

    switch (i){
        case 0:
            return new FirstPage();
        case 1:
            return new SecondPage();
    }

    return null;
}

@Override
public int getCount() {
    return 2;
}

}

So as you saw on the image I attached on this post, where it says hello, I have got an onclick which is in the FirstPage. java file

FirstPage. java file ;

public class FirstPage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.home_screenmain_first, container, false);

    v.findViewById(R.id.first).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
            ((FrameLayout)getActivity().findViewById(android.R.id.tabcontent)).removeAllViews();
            transaction.replace(android.R.id.tabcontent, new ChangeScreen(), "ChangeScreen");
            transaction.commit();
        }
    });

    return v;
}

}

return v;
   }
}

This is a screen show of something from the viewer page is showing on the screen Example of what I am seeing before switching between fragments

This is what I see after switching between fragments After switching between fragments

Please tell me if you want to see any other code, not sure which code to send here that's why I didn't share it on here.

cYrixmorten

Looks like you are creating a ViewPager inside a fragment.

In this case getFragmentManager() should be getChildFragmentManager().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

From Dev

RecyclerView items disappear after switching between fragments

From Dev

Android: Switching between different fragments using tabs

From Dev

Best way to switch between two fragments

From Dev

Switching between Fragments with button

From Dev

Using viewpager with radiogroup to switch between fragments

From Dev

How to automatically change resolution when switching between two monitors using a HDMI hardware switch?

From Dev

Switching between fragments with back button

From Dev

Passing data between two fragments using a callback

From Dev

Passing data between two fragments using a callback

From Dev

Locking a fragment after switching

From Dev

Switching between two div elements using Javascript

From Dev

Switching between two div elements using Javascript

From Dev

How can I switch between two fragments, without recreating the fragments each time?

From Dev

Fragment disappears after setAdapter on ListView

From Dev

Fragment disappears after setAdapter on ListView

From Dev

onItemClickListener between two fragments

From Dev

Communicate between two fragments

From Dev

Communication between two fragments

From Dev

switch between fragments without 'FragmentPagerAdapter'

From Dev

Will Activity onpause be called when switching between fragments

From Dev

NullPointerException when switching between fragments containing RecyclerView

From Dev

Switching between fragments does not call onStop method

From Dev

How to handle camera while switching between fragments?

From Dev

HOW TO Revert Action Bar after Switching Fragments

From Dev

Switching between two storyboards

From Dev

Switching between two images

From Dev

Switching between two storyboards

From Java

Switching between Android Navigation Drawer image and Up caret when using fragments

Related Related

  1. 1

    How to make the toolbar appear when switching between fragments using CoordinatorLayout, Toolbar and fragment

  2. 2

    RecyclerView items disappear after switching between fragments

  3. 3

    Android: Switching between different fragments using tabs

  4. 4

    Best way to switch between two fragments

  5. 5

    Switching between Fragments with button

  6. 6

    Using viewpager with radiogroup to switch between fragments

  7. 7

    How to automatically change resolution when switching between two monitors using a HDMI hardware switch?

  8. 8

    Switching between fragments with back button

  9. 9

    Passing data between two fragments using a callback

  10. 10

    Passing data between two fragments using a callback

  11. 11

    Locking a fragment after switching

  12. 12

    Switching between two div elements using Javascript

  13. 13

    Switching between two div elements using Javascript

  14. 14

    How can I switch between two fragments, without recreating the fragments each time?

  15. 15

    Fragment disappears after setAdapter on ListView

  16. 16

    Fragment disappears after setAdapter on ListView

  17. 17

    onItemClickListener between two fragments

  18. 18

    Communicate between two fragments

  19. 19

    Communication between two fragments

  20. 20

    switch between fragments without 'FragmentPagerAdapter'

  21. 21

    Will Activity onpause be called when switching between fragments

  22. 22

    NullPointerException when switching between fragments containing RecyclerView

  23. 23

    Switching between fragments does not call onStop method

  24. 24

    How to handle camera while switching between fragments?

  25. 25

    HOW TO Revert Action Bar after Switching Fragments

  26. 26

    Switching between two storyboards

  27. 27

    Switching between two images

  28. 28

    Switching between two storyboards

  29. 29

    Switching between Android Navigation Drawer image and Up caret when using fragments

HotTag

Archive