使用值从片段B返回片段A.

时间:2014-11-12 23:43:45

标签: java android android-fragments

我知道有很多关于这个的线索,但我似乎真的不明白如何钉它..我基本上尝试做的是在片段B中按钮点击后我想要与一个一起返回片段A id值..
因此,在我的BackStack中,你会转到片段A,它会引导你进入片段B,当按下一个按钮时,我想回到片段A,并显示一个值。这是片段B中的onClick监听器:

//Fragment B which i called from Fragment A
addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(value.getText().toString().trim().length() != 0) {
                 String order = value.getText().toString(); //The value i want in Fragment B
                 //return to Fragment A
            }
}

在我的MainActivity中,我按照这样处理我的片段:

//fragmentManager is initialized in onCreate()
...
private void selectItem(int position) {

    Fragment fragment = null;
    String fTag = null;

    switch (position) {
        case 0:
            fragment = new FragmentA();
            fTag = "fragmentA";
            break;
        case 1:
            fragment = new FragmentC();
            fTag = "fragmentC";
            break;
        case 2:
            fragment = new FragmentD();
            fTag = "fragmentD";
            break;

        default:
            break;
    }

    if (fragment != null) {
        if(replaceFragments(fragment, fTag)) {
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(mNavigationDrawerItemTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    } else {
        Log.e(LOGTAG, "Error in creating fragment");
    }
}

public boolean replaceFragments(Fragment fragment, String fTag) {
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, fTag).addToBackStack(fTag).commit();
    return true;
}

@Override
public void onBackPressed() {
    int count = fragmentManager.getBackStackEntryCount();
    if (count == 0) {
        super.onBackPressed();
    } else {
        getSupportFragmentManager().popBackStack();
        //TODO: add app title on count == 1
    }
}

2 个答案:

答案 0 :(得分:2)

创建接口ValueChangeListener.java

public interface ValueChangeListener {

    public void onValueChanged(String value);
}

通过界面传递值:

public class FragmentB extends Fragment {

    private ValueChangeListener valueChangeListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            valueChangeListener = (ValueChangeListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement ValueChangeListener");
        }
    }

    //your code
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(value.getText().toString().trim().length() != 0) {
                 String order = value.getText().toString();
                 //now the value is in the Activity and you can pass it anywhere you want from there
                 valueChangeListener.onValueChanged(order);
            }
    }

}

实施活动的界面:

public class YourActivity extends FragmentActivity implements ValueChangeListener{

    @Override
    public void onValueChanged(String value) {
    //your value is here
    }
}

答案 1 :(得分:1)

你的onclick方法应如下所示:

 @Override 
    public void onClick(View view) {
        if(value.getText().toString().trim().length() != 0) { 
             String order = value.getText().toString(); //The value i want in Fragment B
             //return to Fragment A 

            FragmentA fragment = new FragmentA();
            Bundle args=new Bundle();
            args.putString(order);
           fragment.setArguments(args);
           getFragmentManager().beginTransaction().replace((R.id.content_frame,fragment).commit();


        } 

然后从你的FragmentA中你可以用以下方法恢复价值:

getArguments();