0

そしてまず第一に、とにかくあなたの助けに感謝します。これは私にとって難しい質問です。

5つのフラグメントを含むアクティビティがあります。ユーザーインタラクションでフラグメントが交換されます。

ACLを使用しています。

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        stackArray =new ArrayList<Integer>();
        favQ =new ArrayList<Stock>();
        tablet=true;
        mBound = false;
        fragmentActivity = this;
        setContentView(R.layout.splashmain);
        splashfragment =new splashFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction =     fragmentManager.beginTransaction();       
        fragmentTransaction.add(R.id.splashview,splashfragment);
        fragmentTransaction.commit();
        /*
            other stuff....
            */
        fragmentlista = new listafragment();
        fragmentfavourites= new favouritesFragment() ;
        worstbest = new WorstBest();
        searchfragment = new searchFragment(); 
        /*
            other stuff....
            */
        lt = mService.ritira();
        worst=mService.ritiraWorst();
        best=mService.ritiraBest();
        favQ.clear();
        favQ.addAll(mService.ritiraFav());          
            fragmentlista.prendiLista(lt);
        worstbest.prendiListaWorst(worst);
        worstbest.prendiListaBest(best);
        if(favQ.size()>0)fragmentfavourites.prendiLista(favQ);

// --->>>>HERE THE SAME METHOD enableAll() WORKS!!! <---
// --->>>>HERE THE SAME METHOD enableAll() WORKS!!! <---

            splashfragment.enableAll();

// --->>>>HERE THE SAME METHOD enableAll() WORKS!!! <---
// --->>>>HERE THE SAME METHOD enableAll() WORKS!!! <---
        /*
            other stuff....
            */
        }

//Method invoked to setup the configuration of the screen is layoutSchermo(int conf)
public static void layoutSchermo(int conf){
//Check if it is a Tablet in Landscape mode or not
//if it finds v2 than we are on a LARGE screen, then we check ORIENTATIO
    fragmentActivity.setContentView(R.layout.main);
    View v2 =(View)fragmentActivity.findViewById(R.id.view2);
    if(v2==null 
            & 
            fragmentActivity.getResources().getConfiguration().orientation==
            Configuration.ORIENTATION_PORTRAIT)
        tablet=false;

//Calls the screen configuration LIST
if(conf==LIST){     
    fragmentActivity.setContentView(R.layout.main);

    FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.remove(splashfragment);
    fragmentTransaction.commit();
    fragmentManager.executePendingTransactions();
    //Remove old Fragment splashfragment
//At this point I expect the fragment splashfragment is destroyed
//OR NOT???
    fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
    if(!tablet){fragmentTransaction.replace(R.id.view1, fragmentlista);}
    if(tablet){
        fragmentTransaction.replace(R.id.view1, splashfragment);
        fragmentTransaction.replace(R.id.view2,fragmentlista );
        }       fragmentTransaction.addToBackStack(null);   
    stack= fragmentTransaction.commit();
    stackArray.add(stack);
//Brand new fragments added


// --->>>>HERE THE SAME METHOD enableAll() NOT WORKING!!! <---
// --->>>>HERE THE SAME METHOD enableAll() NOT WORKING!!! <---

splashfragment.enableAll(); 
    }

------------

つまり、基本的に何が起こり、どこに問題があるのか​​ということです。

問題はメソッドにあります

layoutSchermo(int conf) 

メソッドlayoutSchermo(int conf)で、

フラグメント(splashfragment)を切り離し、(別のフラグメントと一緒に)再アタッチします。

私が電話するときかどうかは私にはわかりません

 remove(splashfragment)

実際にフラグメントは破壊されているかどうか?

さらに、新しく追加されたフラグメントが新しいフラグメントまたは古いフラグメントである場合は常に、

splashfragment.enableAll();

効果はありませんか?

新しいフラグメントでも古いフラグメントでも機能することを期待しています。

教えてください!

ありがとうmaurizio

----------

編集編集編集編集編集編集 これがフラグメントのコードです(私はそれがあまり役に立たないと思います)

ublic class splashFragment extends Fragment {
    public View v;
    public Button buttonfav;
    public Button buttonBW;
    public Button buttonSe;
    public Button buttonLi;


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        v=inflater.inflate(R.layout.splashnew, container, false);
        RelativeLayout box1 = (RelativeLayout)v.findViewById(R.id.box1);
        //box1.setBackgroundColor(Color.BLUE);
        buttonfav=(Button)v.findViewById(R.id.heart);
        buttonBW=(Button)v.findViewById(R.id.star);
    buttonSe=(Button)v.findViewById(R.id.search);
        buttonLi=(Button)v.findViewById(R.id.lista);

        buttonfav.setBackgroundResource(R.drawable.hearth_gray_tansp);
        buttonBW.setBackgroundResource(R.drawable.star_gray_trans);
        buttonSe.setBackgroundResource(R.drawable.search_gray_transp);
        buttonLi.setBackgroundResource(R.drawable.list_gray_trans);

        buttonfav.setEnabled(false);
        buttonBW.setEnabled(false);
    buttonSe.setEnabled(false);
        buttonLi.setEnabled(false);

        buttonfav.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Quotes.layoutSchermo(Quotes.FAVOURITES);
            }});

        buttonBW.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Quotes.layoutSchermo(Quotes.BESTWORST);
            }});

        buttonSe.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Quotes.layoutSchermo(Quotes.SEARCH);
            }});

        buttonLi.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Quotes.layoutSchermo(Quotes.LIST);
            }});
        return v;           
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {  } 


public void enableAll(){
    buttonfav.setEnabled(true);
    buttonfav.setBackgroundResource(R.drawable.hearth);
    buttonBW.setEnabled(true);
    buttonBW.setBackgroundResource(R.drawable.star);
    buttonLi.setEnabled(true);
    buttonLi.setBackgroundResource(R.drawable.list);
    buttonSe.setEnabled(true);
    buttonSe.setBackgroundResource(R.drawable.search);
}   

}

4

1 に答える 1

1

フラグメントがいつ破壊されるかは、確実に知ることはできません。あなたが知っているのは、onStop() の後、onDetach() の前に呼び出されるということだけです。

あなたのsplashFragment.enableAll()については、そのメソッドが何であるかを示していないので、なぜそれが機能していないのかをどうやって知ることができますか...また、このlayoutSchermoメソッドのより一般的なコンテキストを示していません. 私がこれを言うのは、あなたがこれをすべて間違っていると思うからです. どういうわけかアクティビティを参照する静的メソッドがあります...(それがどのように起こっているのかは明確ではありません)、そのアクティビティ参照にコンテンツビューを設定します..全体がいくつかの赤い旗を立てるだけです。

SplashFragment.enableAll は、その Fragment の onAttach または onResume 内で呼び出す必要がある可能性が最も高いですが、やはり、あなたからの説明がなければ知ることは不可能です。

編集

わかりましたので、あなたはこれについて間違っていると思います。効果的に達成しようとしているのは、フラグメントを再度表示するときに、特定の方法で (状態に応じて) フラグメントを「構成」することです。ここでの問題は、Fragment の View 階層がいつインフレートされるか、またはいつ Activity にアタッチされるかなどを正確に把握できないことです。つまり、単純に基づいてフラグメントの UI に影響を与えるメソッドを呼び出そうとしています。フラグメントのオブジェクトへの参照を持つことは間違いです。Fragment のライフサイクルに接続して、「正しい方法」で処理する必要があります。

私がお勧めするのは、フラグメントの静的コンストラクターを作成して、必要な適切に構成されたフラグメントを簡単に作成できるようにすることです。これは次のようになります。

public class SplashFragment extends Fragment {

    public static SplashFragment newInstance(Bundle bundle) {
        SplashFragment splashFragment = new SplashFragment()
        splashFragment.setArguments(bundle);
        return splashFragment;
    }

    // or alternatively
    public static SplashFragment newInstance(int favResource, int bwResource, int liResource, int seResource,
        boolean favEnabled, boolean bwEnabled, boolean liEnabled, boolean seEnabled) {
        SplashFragment splashFragment = new SplashFragment()
        Bundle bundle = new Bundle();
        bundle.putInt("fav_res", favResource);
        bundle.putInt("bw_res", bwResource);
        bundle.putInt"li_res", liResource);
        bundle.putInt("se_res", seResource);
        bundle.putBoolean("fav_enabled", favEnabled);
        //...And so on  
        splashFragment.setArguments(bundle);
        return splashFragment;
    }

    //Then....

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        //setup your view as normal...then
        buttonFav.setBackgroundResource(getArguments().getInt("fave_res"));
        //.....etc
    }

}

新しいインスタンスを作成せずに Fragment を操作できるようにする必要がある場合、これを行うために私が考える唯一の方法は、次のようにタグを使用してフラグメントを追加することです。

replace(int containerViewId, Fragment fragment, String tag)

add (Fragment fragment, String tag)

品種。

次に、後でフラグメントマネージャにそれらのフラグメントを見つけるように依頼できます。つまり、

SplashFragment splashFragment = (SplashFragment) getFragmentManager().findFragmentByTag("some tag here");

null でないことを確認してから、メソッドを呼び出します...

于 2013-01-16T17:29:45.237 に答える