为什么方法getBackStackEntryCount总是返回0

时间:2017-02-25 09:30:34

标签: java android

我需要计算我的片段,我使用了getbackstackentrycount,但它总是返回0.有人能告诉我我的代码有什么问题吗?

这里是我的代码MainActivity.java

package com.example.bams.taskagain;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.bams.taskagain.Fragment.FragmentOne;

public class MainActivity extends AppCompatActivity {
Button btn;
FragmentManager fm = getFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.fragOne);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fm.beginTransaction().add(R.id.content, new FragmentOne()).addToBackStack("aa").commit();
            //Toast.makeText(getBaseContext(), "text", Toast.LENGTH_LONG).show();
        }
    });

}

}

这是我的fragment班级

package com.example.bams.taskagain.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.bams.taskagain.R;

/**
 * Created by jack on 25/02/2017.
 */

public class FragmentOne extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("Total back stack "+ ((AppCompatActivity)getActivity()).getSupportFragmentManager().getBackStackEntryCount());
    }

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

        return rootView;
       }
    }

有人可以帮我处理我的代码吗? 我只想获得我的fragement所具有的BackStack计数。

1 个答案:

答案 0 :(得分:1)

您正在使用<?php $physicaldata = explode(",",$physical); ?> <select name="PEPhysical[]" multiple onchange="GetrestPhysical(this.value);"> <?php foreach ($physicaldata as $physical) {?> <option <?php if($physical == 'Normal'){ echo ' selected="selected"'; } ?> value="Normal" >Normal Person</option> <option<?php if($physical == 'Deaf/Dumb'){ echo ' selected="selected"'; } ?> value="Deaf/Dumb">Deaf/Dumb</option> <option<?php if($physical == 'Blind'){ echo ' selected="selected"'; } ?> value="Blind">Blind</option> <option<?php if($physical == 'Physically Challenged'){ echo ' selected="selected"'; } ?> value="Physically Challenged">Physically Challenged</option> <option <?php if($physical == 'Mentally Challenged'){ echo ' selected="selected"'; } ?>value="Mentally Challenged">Mentally Challenged</option> <option <?php if($physical == 'With other Disability'){ echo ' selected="selected"'; } ?> value="With other Disability">With other Disability</option> <?php } ?> android.app.Fragment,但使用android.app.FragmentManager来获取堆叠条目数。

使用

getSupportFragmentManager()
如果您的片段为getFragmentManager().getBackStackEntryCount() ,则使用

getFragmentManager(),如果您的片段为android.app.Fragment,则使用getSupportFragmentManager()

有关这些片段的更多信息,请查看Difference between android.app.Fragment and android.support.v4.app.Fragment