更改Tabs并返回后丢失ImageButton资源和监听器

时间:2015-02-14 20:16:38

标签: android android-fragments imagebutton

我有一个有3个滑动Tabs的应用程序,在第一个Tab片段中我创建了一个Imagebutton。 我单击ImageButton并更改Image资源。 当滑动到第二个Tab并返回图像资源时保持正常,但如果我滑动到3选项卡并返回到第一个,ImageButton将更改为原始状态并且侦听器丢失(如果我尝试单击ImageButton,没有clicklistener是活跃的!)

这是我的代码: 编辑更新代码

@Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            Log.d(TAG, "onSaveInstanceState");
        }

    @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                rootView = inflater.inflate(R.layout.page1, container, false);
                setRetainInstance(true);
                Log.d(TAG, "onCreateView step 1");
                if(btn == null)
                    btn = (ImageButton)rootView.findViewById(R.id.btnP1C1R1);
                btn.setOnClickListener(listener);
        return rootView;
            }

    @Override
            public void onViewCreated(View v, Bundle savedInstanceState) {
                super.onViewCreated(v, savedInstanceState);
                Log.d(TAG, "onViewCreated step 2");

            }

    @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                Log.d(TAG, "onActivityCreated step 3"); 
                super.onActivityCreated(savedInstanceState);
                appContext = getActivity().getBaseContext();
                appContextDialog = getActivity();
                GappContext = appContext.getApplicationContext();      
            }

    ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                btn.setImageResource( R.drawable.delete_64 );
                Toast.makeText(appContext, "click", Toast.LENGTH_LONG).show();
            }
        };

the sequence is:
Pagina3﹕ onCreate step 0
Pagina3﹕ onCreateView step 1
Pagina3﹕ onViewCreated step 2
Pagina3﹕ onActivityCreated step 3
Pagina3﹕ On Start
Pagina3﹕ On Resume .....
Pagina1﹕ On Pause .....
Pagina1﹕ On Stop .....
Pagina1﹕ onCreateView step 1
Pagina1﹕ onViewCreated step 2
Pagina1﹕ onActivityCreated step 3
Pagina1﹕ On Start
Pagina1﹕ On Resume .....
Pagina3﹕ On Pause .....
Pagina3﹕ On Stop .....

Page1是Tab1上的片段,Page3是Tab3上的片段。 怎么解决呢?

我已使用此help

解决了这个问题

0 个答案:

没有答案