使用接口将数据从Activity传递到Fragment

时间:2016-05-09 16:41:41

标签: android android-fragments interface

我想使用interface no bundle将数据从活动传递到bundle。 请看下面的代码:

接口:

public interface FragmentCommunicator {

   public void passData(String name);
}

MainActivity

public class MainActivity extends AppCompatActivity{

    FragmentCommunicator fragmentCommunicator;
    private Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button) findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragment= new Fragment();
                fragmentCommunicator = (FragmentCommunicator) getApplication();
                fragmentCommunicator.passData("hello");
                getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment).commit();

            }
        });

    }
}

片段

public class Fragment extends android.support.v4.app.Fragment implements FragmentCommunicator {

    FragmentCommunicator communicator;
    Context c;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, null);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.c = context;

    }
    @Override
    public void passData(String name) {
        Toast.makeText(c, name, Toast.LENGTH_SHORT).show();
    }
}

我只想在点击按钮启动片段时传递一些字符串,当片段启动时应显示包含该字符串的吐司.. 请帮忙 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

在onCreate方法之后写下这行代码。

public void passVal(FragmentCommunicator fragmentCommunicator) {
    this.fragmentCommunicator = fragmentCommunicator;
}

像这样的东西

public class MainActivity extends AppCompatActivity{

FragmentCommunicator fragmentCommunicator;
private Fragment fragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button);

    fragment= new Fragment();

    //App is crasing for this line. Working fine by removing it
    //fragmentCommunicator = (FragmentCommunicator) getApplication();

    //fragmentCommunicator.passData("hello");
    getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment).commit();

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            fragmentCommunicator.passData("Hello");

        }
    });

  }

   //Here is new method
   public void passVal(FragmentCommunicator fragmentCommunicator) {
       this.fragmentCommunicator = fragmentCommunicator;

   }
}

然后将这行代码写入片段的onCreateView()。像这样的东西

@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup  container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, null);

((MainActivity) getActivity()).passVal(new FragmentCommunicator() {
       @Override
       public void passData(String name) {
           Toast.makeText(c, name, Toast.LENGTH_SHORT).show();
       }
   });
  return view;
}

注意:无需在片段中实现FragmentCommunicator接口。希望它有效。这个对我有用。我已经测试了

答案 1 :(得分:1)

活动包含片段,您不需要传递任何内容。一种技术是将数据存储在活动范围内,并使用getActivity()获取对它的引用。

Best practice to reference the parent activity of a fragment?

就您发布的代码而言,我看不到您在片段中调用传递数据方法的位置。我建议在onviewcreated()中调用它。

我将尝试稍后提供一些示例代码,因为在我的移动设备上执行此操作非常困难。

尝试passdata(getActivity()。someStringVariable);

答案 2 :(得分:0)

Fragment课程中,有一个TAG字段。命名片段Fragment可能有点令人困惑,因为它与Android Fragment的名称相同,因此我将使用ExampleFragment

public class ExampleFragment extends android.support.v4.app.Fragment implements FragmentCommunicator {
    public static final String TAG = "ExampleFragment";

    // ...
}

现在,当您替换活动中的片段时,请务必通过TAG

getSupportFragmentManager().beginTransaction().replace(R.id.container ,fragment, ExampleFragment.TAG).commit();

请注意,在您的情况下,它会是Fragment.TAG,但我使用ExampleFragment来澄清Fragment.TAG不是Android SDK的一部分。

要在您的活动中获取您的片段的引用,请使用FragmentManager查找您的片段:

Fragment myFragment = getSupportFragmentManager().findFragmentByTag(ExampleFragment.TAG);

然后,您可以确保myFragment不是null并将其投放到FragmentCommunicator

if (myFragment != null) {
    fragmentCommunicator = (FragmentCommunicator) myFragment;
}

请注意,在您的示例中fragmentCommunicator = (FragmentCommunicator) getApplication();无效,因为它是实现接口的片段,而不是您的应用程序。