Handle orientation changes of ViewPager












1















I have already searched the entire Internet, but could not solve my problem.
There is a fragment with an adapter:



@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CURRENT_TAB = viewPager.getCurrentItem();
outState.putInt("current_tab", CURRENT_TAB);
}


Okey, there is good, my current page= 2 saved.
Then i have:



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppPreferences appPreferences = new AppPreferences(getContext());

if (savedInstanceState != null) {
CURRENT_TAB = savedInstanceState.getInt("current_tab");
viewPager.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(CURRENT_TAB);
}
});
}


......



public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
view = inflater.inflate(R.layout.fragment_orderlist, container, false);
savedInstanceState.getString("current_tab"));


In both situations my savedInstanceState is null. What is wrong?










share|improve this question

























  • refer this link stackoverflow.com/questions/18075853/…

    – Android Team
    Nov 22 '18 at 5:23











  • @AndroidTeam, how FragmentManager allows save my bundle object?...

    – AlexS
    Nov 22 '18 at 5:26











  • set into argument

    – Android Team
    Nov 22 '18 at 5:32











  • refer this link stackoverflow.com/questions/15313598/…

    – Android Team
    Nov 22 '18 at 5:33











  • why did you write (Dublicate) in title ?

    – Manohar Reddy
    Nov 22 '18 at 6:14


















1















I have already searched the entire Internet, but could not solve my problem.
There is a fragment with an adapter:



@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CURRENT_TAB = viewPager.getCurrentItem();
outState.putInt("current_tab", CURRENT_TAB);
}


Okey, there is good, my current page= 2 saved.
Then i have:



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppPreferences appPreferences = new AppPreferences(getContext());

if (savedInstanceState != null) {
CURRENT_TAB = savedInstanceState.getInt("current_tab");
viewPager.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(CURRENT_TAB);
}
});
}


......



public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
view = inflater.inflate(R.layout.fragment_orderlist, container, false);
savedInstanceState.getString("current_tab"));


In both situations my savedInstanceState is null. What is wrong?










share|improve this question

























  • refer this link stackoverflow.com/questions/18075853/…

    – Android Team
    Nov 22 '18 at 5:23











  • @AndroidTeam, how FragmentManager allows save my bundle object?...

    – AlexS
    Nov 22 '18 at 5:26











  • set into argument

    – Android Team
    Nov 22 '18 at 5:32











  • refer this link stackoverflow.com/questions/15313598/…

    – Android Team
    Nov 22 '18 at 5:33











  • why did you write (Dublicate) in title ?

    – Manohar Reddy
    Nov 22 '18 at 6:14
















1












1








1








I have already searched the entire Internet, but could not solve my problem.
There is a fragment with an adapter:



@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CURRENT_TAB = viewPager.getCurrentItem();
outState.putInt("current_tab", CURRENT_TAB);
}


Okey, there is good, my current page= 2 saved.
Then i have:



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppPreferences appPreferences = new AppPreferences(getContext());

if (savedInstanceState != null) {
CURRENT_TAB = savedInstanceState.getInt("current_tab");
viewPager.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(CURRENT_TAB);
}
});
}


......



public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
view = inflater.inflate(R.layout.fragment_orderlist, container, false);
savedInstanceState.getString("current_tab"));


In both situations my savedInstanceState is null. What is wrong?










share|improve this question
















I have already searched the entire Internet, but could not solve my problem.
There is a fragment with an adapter:



@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
CURRENT_TAB = viewPager.getCurrentItem();
outState.putInt("current_tab", CURRENT_TAB);
}


Okey, there is good, my current page= 2 saved.
Then i have:



@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppPreferences appPreferences = new AppPreferences(getContext());

if (savedInstanceState != null) {
CURRENT_TAB = savedInstanceState.getInt("current_tab");
viewPager.post(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(CURRENT_TAB);
}
});
}


......



public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
view = inflater.inflate(R.layout.fragment_orderlist, container, false);
savedInstanceState.getString("current_tab"));


In both situations my savedInstanceState is null. What is wrong?







android android-viewpager bundle orientation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:15







AlexS

















asked Nov 22 '18 at 5:19









AlexSAlexS

194111




194111













  • refer this link stackoverflow.com/questions/18075853/…

    – Android Team
    Nov 22 '18 at 5:23











  • @AndroidTeam, how FragmentManager allows save my bundle object?...

    – AlexS
    Nov 22 '18 at 5:26











  • set into argument

    – Android Team
    Nov 22 '18 at 5:32











  • refer this link stackoverflow.com/questions/15313598/…

    – Android Team
    Nov 22 '18 at 5:33











  • why did you write (Dublicate) in title ?

    – Manohar Reddy
    Nov 22 '18 at 6:14





















  • refer this link stackoverflow.com/questions/18075853/…

    – Android Team
    Nov 22 '18 at 5:23











  • @AndroidTeam, how FragmentManager allows save my bundle object?...

    – AlexS
    Nov 22 '18 at 5:26











  • set into argument

    – Android Team
    Nov 22 '18 at 5:32











  • refer this link stackoverflow.com/questions/15313598/…

    – Android Team
    Nov 22 '18 at 5:33











  • why did you write (Dublicate) in title ?

    – Manohar Reddy
    Nov 22 '18 at 6:14



















refer this link stackoverflow.com/questions/18075853/…

– Android Team
Nov 22 '18 at 5:23





refer this link stackoverflow.com/questions/18075853/…

– Android Team
Nov 22 '18 at 5:23













@AndroidTeam, how FragmentManager allows save my bundle object?...

– AlexS
Nov 22 '18 at 5:26





@AndroidTeam, how FragmentManager allows save my bundle object?...

– AlexS
Nov 22 '18 at 5:26













set into argument

– Android Team
Nov 22 '18 at 5:32





set into argument

– Android Team
Nov 22 '18 at 5:32













refer this link stackoverflow.com/questions/15313598/…

– Android Team
Nov 22 '18 at 5:33





refer this link stackoverflow.com/questions/15313598/…

– Android Team
Nov 22 '18 at 5:33













why did you write (Dublicate) in title ?

– Manohar Reddy
Nov 22 '18 at 6:14







why did you write (Dublicate) in title ?

– Manohar Reddy
Nov 22 '18 at 6:14














1 Answer
1






active

oldest

votes


















0














   // First extend your class with Fragment like

public class Abc extends Fragment {

private View rootView;
private static CustomViewPager mPager;
private static int currentPage = 0;

// Then Required an empty public constructor

public Abc() {

}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
//Now initialize the viewpager and do the magic

mPager = rootView.findViewById(R.id.pager);
mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



mPager.setPagingEnabled(false);
int NUM_PAGES = ArrayList.size();

return rootView;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424311%2fhandle-orientation-changes-of-viewpager%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














       // First extend your class with Fragment like

    public class Abc extends Fragment {

    private View rootView;
    private static CustomViewPager mPager;
    private static int currentPage = 0;

    // Then Required an empty public constructor

    public Abc() {

    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
    //Now initialize the viewpager and do the magic

    mPager = rootView.findViewById(R.id.pager);
    mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



    mPager.setPagingEnabled(false);
    int NUM_PAGES = ArrayList.size();

    return rootView;





    share|improve this answer




























      0














         // First extend your class with Fragment like

      public class Abc extends Fragment {

      private View rootView;
      private static CustomViewPager mPager;
      private static int currentPage = 0;

      // Then Required an empty public constructor

      public Abc() {

      }

      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
      //Now initialize the viewpager and do the magic

      mPager = rootView.findViewById(R.id.pager);
      mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



      mPager.setPagingEnabled(false);
      int NUM_PAGES = ArrayList.size();

      return rootView;





      share|improve this answer


























        0












        0








        0







           // First extend your class with Fragment like

        public class Abc extends Fragment {

        private View rootView;
        private static CustomViewPager mPager;
        private static int currentPage = 0;

        // Then Required an empty public constructor

        public Abc() {

        }

        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
        //Now initialize the viewpager and do the magic

        mPager = rootView.findViewById(R.id.pager);
        mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



        mPager.setPagingEnabled(false);
        int NUM_PAGES = ArrayList.size();

        return rootView;





        share|improve this answer













           // First extend your class with Fragment like

        public class Abc extends Fragment {

        private View rootView;
        private static CustomViewPager mPager;
        private static int currentPage = 0;

        // Then Required an empty public constructor

        public Abc() {

        }

        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_amazing_hero, container, false);
        //Now initialize the viewpager and do the magic

        mPager = rootView.findViewById(R.id.pager);
        mPager.setAdapter(new Your_Adapter(getActivity(), ArrayList));



        mPager.setPagingEnabled(false);
        int NUM_PAGES = ArrayList.size();

        return rootView;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:51









        Suvarthee ChakravartiSuvarthee Chakravarti

        11




        11
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424311%2fhandle-orientation-changes-of-viewpager%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?