开发者

Save screenId for onCreate

开发者 https://www.devze.com 2023-03-19 19:12 出处:网络
I have a class which reads an XML file to create several screens in a Horizontal Page flipper. However, the view content can be different when changing from portrait to landscape or back.

I have a class which reads an XML file to create several screens in a Horizontal Page flipper. However, the view content can be different when changing from portrait to landscape or back.

I'd like to save the current selected page so whenever I swap my screen it uses that value again to build up the new orientation screen.

I us开发者_开发问答e a HorizontalPager class which can retrieve current page by getCurrentScreen() (returns an integer) and one to set the page setCurrentScreen(int currentScreen, boolean animate)

So whenever I switch screens, the current page should still be selected, instead of creating the whole thing again and starting from page 1 (0 in the array).


Save the index of your page before a screen orientation change takes place:

@Override
public Object onRetainNonConfigurationInstance() {
    return mIndex;//your index
}

In onCreate():

Integer index= (Integer) getLastNonConfigurationInstance();

If index is not null, then you have some data saved before the screen orientation changed, and should use it, otherwise ignore it.


I just slept over my question and solved it myself :)

What I did is:

I made a method called setPager(int pager) that sets my static in Pager to the current page. This method is called from the HorizontalPager class whenever a screen is flipped.

Every time the screen flips, the onCreate() is called, and my HorizontalPager will be set to the value my static int has with a method setPager(int pager).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号