com.cnm.cnmrc.fragment.vodtvch.Base.java Source code

Java tutorial

Introduction

Here is the source code for com.cnm.cnmrc.fragment.vodtvch.Base.java

Source

/*
 * Copyright (C) 2011 Chris Gao <chris@exina.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cnm.cnmrc.fragment.vodtvch;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

import com.cnm.cnmrc.MainActivity;
import com.cnm.cnmrc.R;
import com.cnm.cnmrc.fragment.rc.RcBottomMenu;

public class Base extends Fragment {

    public Base newInstance(int selectedCategory, String title, boolean isFirstDepth, Bundle bundle) {
        Base f = new Base();
        return f;
    }

    boolean isFirstDepth = true; // ? true?. , findFragmentById(R.id.loading_data_panel)? ? ???.
    // false : findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_BASE)? ?? ? depth ? .
    String[] mClassTypeArray = { "VodList", "VodSemi", "VodDetail", "TvchList", "TvchSemi", "TvchDetail" };

    @Override
    public void onDestroyView() {
        {
            // back key ? 2,    ?,  destoryview? ? count minus?.
            // ? TAG_FRAGMENT_BASE?  fragment ?  .  . .
            // backstack?  transaction? count ? minus ?.
            Fragment f = getActivity().getSupportFragmentManager()
                    .findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
            if (f != null) {
                Log.i("hwang", "At Base TAG_FRAGMENT_BASE exist!!!");
            } else {
                Log.i("hwang", "At Base TAG_FRAGMENT_BASE no exist!!!");
            }

            f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
            if (f != null) {
                Log.i("hwang", "At Base loading_data_panel exist!!!");
            } else {
                Log.i("hwang", "At Base loading_data_panel no exist!!!");
            }
        }

        //      {
        //         FragmentManager fm = getActivity().getSupportFragmentManager();
        //         Log.i("hwang", "before destory base view fragment count --> " + Integer.toString(fm.getBackStackEntryCount()));
        //           Fragment f = getActivity().getSupportFragmentManager().findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
        //           if (f != null) {
        //              getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();   // TAG_FRAGMENT_BASE?  fragment ?? ?.
        //            fm.executePendingTransactions(); // fragment ?  ?.
        //           }
        //         Log.i("hwang", "after destory base view fragment count --> " + Integer.toString(fm.getBackStackEntryCount()));
        //         
        //         // check
        //         //  entry stack? ? 0 ? index?.
        //         for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
        //            Log.i("hwang", "Found fragment: " + fm.getBackStackEntryAt(entry).getId());
        //         }
        //      }

        //      {
        //         // same above, ~ ?? ? TAG_FRAGMENT_BASE? ?  fragment ? ?.
        //         FragmentManager fm = getActivity().getSupportFragmentManager();
        //         Log.i("hwang", "before destory base view fragment count --> " + Integer.toString(fm.getBackStackEntryCount()));
        //           Fragment f = getActivity().getSupportFragmentManager().findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
        //           if (f != null) {
        //            fm.popBackStack(((MainActivity) getActivity()).TAG_FRAGMENT_BASE, 0);   // ? ? null ?.             // TAG_FRAGMENT_BASE?  fragment  ?.
        //             fm.popBackStack(((MainActivity) getActivity()).TAG_FRAGMENT_BASE, FragmentManager.POP_BACK_STACK_INCLUSIVE);   // TAG_FRAGMENT_BASE?  fragment  ?. ??.....
        //            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);                                    // stack?   fragment ?.
        //           }
        //         Log.i("hwang", "after destory base view fragment count --> " + Integer.toString(fm.getBackStackEntryCount()));
        //         
        //         // check
        //         //  entry stack? ? 0 ? index?.
        //         for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
        //            Log.i("hwang", "Found fragment: " + fm.getBackStackEntryAt(entry).getId());
        //         }
        //      }

        //      To clear stack you need to call:
        //
        //         ScreenFragment.sDisableExitAnimation = true;
        //         manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        //         transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        //      FragmentManager fm = getActivity().getSupportFragmentManager();
        //      for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {    
        //          fm.popBackStack();
        //      }
        //      But could equally have used something like:
        //
        //      FragmentManager.popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        //      To go to top simply use: fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

        //      // transaction.replace(R.id.detailFragment, frag1);
        //      Transaction.remove(null).add(frag1)  // frag1 on view
        //
        //      // transaction.replace(R.id.detailFragment, frag2).addToBackStack(null);
        //      Transaction.remove(frag1).add(frag2).addToBackStack(null)  // frag2 on view
        //
        //      // transaction.replace(R.id.detailFragment, frag3);
        //      Transaction.remove(frag2).add(frag3)  // frag3 on view

        //      FragmentManager fm = getActivity().getSupportFragmentManager();
        //      for(int i = 0; i < fm.getBackStackEntryCount(); ++i) {
        //          fm.popBackStack();
        //      }

        // Use a name for your initial back stack state and use FragmentManager.popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE).
        // Use FragmentManager.getBackStackEntryCount()/getBackStackEntryAt().getId() to retrieve the ID of the first entry on the back stack, 
        // and FragmentManager.popBackStack(int id, FragmentManager.POP_BACK_STACK_INCLUSIVE).

        // is supposed to pop the entire back stack...
        //FragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) 

        // Base? onDestroyView() 2  .
        //  ft.replace(R.id.loading_data_panel, base); 
        //   ft.add(R.id.loading_data_panel, base, ((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
        // getBackStackEntryCount() ??  ...  ?  ? ? ??...
        // backstack? count  ???  ? ?...
        FragmentManager fm = getActivity().getSupportFragmentManager();
        if (!isFirstDepth) { //  1 depth?? ? depth?...
            //if(fm.getBackStackEntryCount() > 0 && fm.getBackStackEntryCount() < 8) {     // indicator 8 ...
            Log.i("hwang", "before Base onDestroyView() fragment count --> "
                    + Integer.toString(fm.getBackStackEntryCount()));
            Fragment f = getActivity().getSupportFragmentManager()
                    .findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
            if (f != null) {
                //  ?? ~~~~
                // popBackStack()? ?  ? ? ? ?. , backstack count ? minus? ??.
                //fm.popBackStack(((MainActivity) getActivity()).TAG_FRAGMENT_BASE, 0);   // ? ? null ?.               // TAG_FRAGMENT_BASE?  fragment  ?.
                //fm.popBackStack(((MainActivity) getActivity()).TAG_FRAGMENT_BASE, FragmentManager.POP_BACK_STACK_INCLUSIVE);   // TAG_FRAGMENT_BASE?  fragment  ?. ??...
                //fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);                                    // stack?   fragment ?.

                //getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();                        // TAG_FRAGMENT_BASE?  fragment ?? ?.
                //fm.executePendingTransactions();                                                             // fragment ?  ?.

                // sidebar ? ...
                Fragment f1 = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);
                if (f1 != null) {
                    if (((VodTvchMain) f1).mSlidingMenu.isOpening()) {
                        getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
                        //fm.getBackStackEntryAt(0);
                        //fm.popBackStack(((MainActivity) getActivity()).TAG_FRAGMENT_BASE, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        //fm.executePendingTransactions();    

                        for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
                            //                       fm.popBackStack(((MainActivity) getActivity()).TAG_FRAGMENT_BASE, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                            fm.popBackStack(i, 0); // OK
                            //fm.popBackStack(i, FragmentManager.POP_BACK_STACK_INCLUSIVE); // not OK
                        }
                    }
                }

            }
            Log.i("hwang", "after Base onDestroyView() fragment count --> "
                    + Integer.toString(fm.getBackStackEntryCount()));

            // check
            //  entry stack? ? 0 ? index?.
            for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
                Log.i("hwang", "At Base onDestroyView() Found fragment: " + fm.getBackStackEntryAt(entry).getId());
            }

            deleteDepthLevel();

            {
                f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
                FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
                if (f != null) {
                    if (f.isHidden())
                        ft.show(f).commit(); // loading_data_panel TAG_FRAGMENT_BASE  ?.   ?
                    // f --> VodList{42626a88 #12 id=0x7f0c0048 base}
                    // f --> VodList{42624ab0 #11 id=0x7f0c0048 base}
                    // f --> VodList{42622ad8 #10 id=0x7f0c0048 base}
                    // f --> VodList{42620b00 #9 id=0x7f0c0048 base}
                    // f --> VodList{4261ea88 #8 id=0x7f0c0048 base}
                    // f --> VodList{4261c560 #7 id=0x7f0c0048}
                }
            }

        }

        super.onDestroyView();
    }

    /**
     * ? depth?  fragment Base ?? loadingData()? addToBackStack?  ?.
     * ? MainActivity onBackPressed()? back key ?.
     *   ? isFirstDepth false .  4? ?(,?,TV,)? .
     * vod (1st selectedCategory arg : 2nd title arg) : (0:) / (1:?) / (2:TV) / (3:)
     */
    protected void loadingData(int selectedCategory, String title, boolean isFirstDepth, Bundle bundle) {

        //    ft.replace(R.id.loading_data_panel, base)? base?
        //  base? isSldebar? ? ?,  ?  ?, newInstance() ? .
        // this.isFirstDepth = isFirstDepth;

        // 1 depth?  ft.replace(R.id.loading_data_panel, base)? base? ?? hide.
        if (this.isFirstDepth) { // ? ??   ?? ?  ? ??.
            Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.loading_data_panel);
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            if (f != null) {
                if (f.isVisible())
                    ft.hide(f).commit();
            }

            if (!isFirstDepth)
                setTitle(title);
        } else {
            Fragment f = getActivity().getSupportFragmentManager()
                    .findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            if (f != null) {
                if (f.isVisible())
                    ft.hide(f).commit();
            }

            setTitle(title);
        }

        FragmentManager fm = getActivity().getSupportFragmentManager();
        Log.i("hwang", "before adding base fragment count --> " + Integer.toString(fm.getBackStackEntryCount()));

        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        Class<?> classObject;
        try {
            String packageName = this.getClass().getPackage().getName() + ".";

            classObject = Class.forName(packageName + mClassTypeArray[selectedCategory]);
            Object obj = classObject.newInstance();
            Log.v("hwang", "classObject : " + obj.getClass());

            Base base = ((Base) obj).newInstance(selectedCategory, title, isFirstDepth, bundle);

            Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.vod_tvch_panel);
            if (f != null) {

                ft.add(R.id.loading_data_panel, base, ((MainActivity) getActivity()).TAG_FRAGMENT_BASE);
                ft.addToBackStack(null); // push to "fragment stack" for back key operation
            }
            ft.commit();
            fm.executePendingTransactions();

            // ------------------------------------
            // ? bottom menu? depth level 
            // ------------------------------------
            addDepthLevel();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        FragmentManager fm1 = getActivity().getSupportFragmentManager();
        Log.i("hwang", "after adding base fragment count --> " + Integer.toString(fm1.getBackStackEntryCount()));
    }

    protected void addDepthLevel() {
        // ------------------------------------
        // ? bottom menu? depth level 
        // 2 depth  ?.
        // ------------------------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            ((RcBottomMenu) f).addDepthLevel(fm.getBackStackEntryCount() - 1); // ft.add() ?? ?  backstack count ? ?.  -1 .
        }
    }

    protected void deleteDepthLevel() {
        // ------------------------------------
        // ? bottom menu? depth level 
        // 2 depth  ?.
        // ------------------------------------
        Fragment f = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_rc_bottom_menu);
        if (f != null) {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            ((RcBottomMenu) f).deleteDepthLevel(fm.getBackStackEntryCount());
        }

        setTitleBackKey();
    }

    //  ?? VodTvch Base ? ? ?. (2)
    void setTitle(String title) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);
        if (f != null)
            ((VodTvchMain) f).setTitle(title);
    }

    private void setTitleBackKey() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);
        if (f != null)
            ((VodTvchMain) f).setTitle();
    }

    protected void increaseCurrentDepth() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);
        if (f != null)
            ((VodTvchMain) f).currentDepth++;
    }

    protected void decreaseCurrentDepth() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);
        if (f != null)
            ((VodTvchMain) f).currentDepth--;
    }

    protected int getCurrentDepth() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);
        if (f != null)
            return ((VodTvchMain) f).currentDepth;
        else
            return 0;
    }

    protected Fragment getVodTvchFragment() {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(((MainActivity) getActivity()).TAG_FRAGMENT_VOD_TVCH);

        return f;
    }

}