参考Fragment

时间:2016-06-07 13:21:47

标签: android android-fragments android-activity fragmentmanager

问题:很少有代码示例无法帮助我获取对Fragment的引用。它总是返回null。

我做了什么:我有一个包含片段的活动。该片段进行了网络连接。如果已完成加载,我在父活动中调用onNetworkingFinished方法。

onNetworkingFinished方法的内部,我需要获得对片段的引用,以便能够调用片段的方法。

该片段肯定不为null,因为onNetworkingFinished方法被它调用...

public class MainActivity extends AppCompatActivity implements MaschineFragment.Callbacks{

    private Toolbar toolbar;
    private FloatingActionButton mFab;
    private CoordinatorLayout mCordinatorLayout;
    private MaschineFragment mMaschineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.maschinelistcontainer, new MaschineFragment(), "maschinefragment");

        if(getResources().getBoolean(R.bool.dual_pane)){
            ft.add(R.id.detailreviewcontainer, new ReviewMaschineFragment(), "detailfragment");
        }

        ft.commit();

        mCordinatorLayout = (CoordinatorLayout) findViewById(R.id.maschineCoordinatorLayout);
        mFab = (FloatingActionButton) findViewById(R.id.fab_download_maschinen);
        mFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    @Override
    public void onNetworkingFinished(boolean finishedWithError) {
        if(finishedWithError){
            Log.i("Networking", "Daten NUR lokal geladen");
            Snackbar snackbar = Snackbar
                    .make(mCordinatorLayout, getResources().getString(R.string.networking_finished_with_error), Snackbar.LENGTH_LONG);
            snackbar.show();
        } else {
            Log.i("Networking", "Fertig geladen");
            Snackbar snackbar = Snackbar
                    .make(mCordinatorLayout, getResources().getString(R.string.networking_finished_with_error), Snackbar.LENGTH_LONG);
            snackbar.show();
        }

        mMaschineFragment = (MaschineFragment) getSupportFragmentManager().findFragmentById(R.id.maschine_fragment);
        if (mMaschineFragment==null || ! mMaschineFragment.isInLayout()) {
            Log.i("Maschine Fragment", "NULL und nicht im Layout");
        }
        else {
            Log.i("Maschine Fragment", "VORHANDEN");
        }



    }

 (...)


}

3 个答案:

答案 0 :(得分:0)

您必须将片段引用传递给onNetworkingFinished方法。

示例:您必须通过someobject.onNetworkingFinished(boolean finishedWithError,YOURFRAGMENTNAME.this)从片段调用onNetworkingFinished方法;

在活动中实施 onNetworkingFinished 方法

 @Override
    public void onNetworkingFinished(boolean finishedWithError,YOURFRAGMENTNAME obj)
 {
  //here you can have fragment access in obj
}

答案 1 :(得分:0)

我认为你误解了findFragmentById()。此方法获取的参数是您之前添加片段的View的id,而不是片段的id。

您添加了这样的片段:

ft.add(R.id.maschinelistcontainer, new MaschineFragment(), "maschinefragment");

所以你应该尝试像这样得到你的片段:

mMaschineFragment = (MaschineFragment) getSupportFragmentManager().findFragmentById(R.id.maschinelistcontainer);

由于您已指定了标记,因此您也可以通过标记获取标记:

mMaschineFragment = (MaschineFragment) getSupportFragmentManager().findFragmentByTag("maschinefragment");

答案 2 :(得分:0)

而不是这个 ft.add(R.id.maschinelistcontainer,新的MaschineFragment()," maschinefragment");

试试这个

mMaschineFragment = new MaschineFragment(); ft.add(R.id.maschinelistcontainer,mMaschineFragment," maschinefragment");

然后通过全局对象 mMaschineFragment 访问它;

您也可以通过提供对调用片段的引用来调用 onNetworkingFinished 函数,如:

@Override
public void onNetworkingFinished(boolean finishedWithError, MaschineFragment fragment) {
    if(finishedWithError){
        Log.i("Networking", "Daten NUR lokal geladen");
        Snackbar snackbar = Snackbar
                .make(mCordinatorLayout, getResources().getString(R.string.networking_finished_with_error), Snackbar.LENGTH_LONG);
        snackbar.show();
    } else {
        Log.i("Networking", "Fertig geladen");
        Snackbar snackbar = Snackbar
                .make(mCordinatorLayout, getResources().getString(R.string.networking_finished_with_error), Snackbar.LENGTH_LONG);
        snackbar.show();
    }

    mMaschineFragment = fragment;
    if (mMaschineFragment==null || ! mMaschineFragment.isInLayout()) {
        Log.i("Maschine Fragment", "NULL und nicht im Layout");
    }
    else {
        Log.i("Maschine Fragment", "VORHANDEN");
    }



}