难道片段调用父活动的onCreate方法? [英] Does fragment call the onCreate method of parent activity?

查看:121
本文介绍了难道片段调用父活动的onCreate方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它里面一个viewPager的活动,而且我在活动的onCreate方法,使用 Col​​lections.shuffle(名单)洗牌整数静态的ArrayList,这viewPager的片段使用父活动ArrayList中。

I have an activity with a viewPager inside of it, and a static ArrayList of integers that I am shuffling using Collections.shuffle(list) in the activity's onCreate method, this viewPager's fragments are using the ArrayList in parent activity.

问题是,每当实例化viewPager的父活动的onCreate方法一个新片段被调用,我不希望这样的事情发生,因为我想要列表在所有片段相同的数据,而不是重新洗牌。做片段致电其父母活动的onCreate方法每次有新的实例?如果是的,我怎么能解决这个使列表洗牌,从每一次?

The problem is that whenever a new fragment instantiated of the viewPager the onCreate method of parent activity is called, and I don't want that to happen because I want the list to have the same data in all fragments and not reshuffled. Do fragments call the onCreate method of their parent activities everytime there is a new instance? if Yes how can I work around this to keep the list from shuffling every time?

code:

活动code:

public static final ArrayList<Integer> IDs = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        IDs.add(0);
        IDs.add(1);
        IDs.add(2);

        Collections.shuffle(IDs);

        setContentView(R.layout.activity_walkthrough);
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        pager.setAdapter(adapter);

片段code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = (View) inflater.inflate(
                R.layout.fragment_walkthrough, container, false);

        final TypedArray imgs = getResources().obtainTypedArray(R.array.walkthrough_images);


        ImageView imageView = (ImageView) v.findViewById(R.id.image);
        if (page == 0) {
            imageView.setImageResource(imgs.getResourceId(Walkthrough.IDs.get(0), 0));
        } else if (page == 2) {
            imageView.setImageResource(imgs.getResourceId(Walkthrough.IDs.get(1), 0));
        } else {
            imageView.setImageResource(imgs.getResourceId(Walkthrough.IDs.get(2), 0));
        }

        return v;

    }

现在我想ArrayList的标识总是有相同的数据,以便当过我实例化一个新片段,但它不工作,我每次创建一个新片段的onCreate被召回的方法和洗牌发生!

Now I want the ArrayList "IDs" to always have the same data and order when ever I instantiate a new fragment but it is not working, every time I create a new fragment the method onCreate gets recalled and a reshuffle happens!

推荐答案

片段加入到活动,因此片段得到受活动。
活动会导致调用任何片段回调方法,但片段不能

Fragments are added to activity and therefore fragments get affected by activity. Activity can cause calling any fragment callback method, but fragment can't

,其中片段住直接活性的生命周期影响片段的生命周期。

The lifecycle of the activity in which the fragment lives directly affects the lifecycle of the fragment.

例如,当活动接收的onPause(),在活动的每个片断接收的onPause()

For example, when the activity receives onPause(), each fragment in the activity receives onPause().

片段有一些额外的生命周期回调,但是,处理以执行诸如生成动作和破坏片段的UI与活动独特相互作用。
 这些额外的回调方法就像onAttach(),onCreateView()等。

Fragments have a few extra lifecycle callbacks, however, that handle unique interaction with the activity in order to perform actions such as build and destroy the fragment's UI. These additional callback methods are like onAttach(), onCreateView(), etc.

这将清除碎片和活动之间的关系有所

It'll clears the somewhat relation between fragment and activity.

感谢

这篇关于难道片段调用父活动的onCreate方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆