Android 导航抽屉中碎片之间的通信

Android 导航抽屉中碎片之间的通信,android,android-fragments,Android,Android Fragments,我试图在点击按钮时将数据从片段A发送到导航抽屉的片段B。我尝试了捆绑和意图,但两者都不起作用 在片段A中,我有editText和按钮,当我单击时,数据被传递到另一个片段 在片段B中有一个文本视图,其中将显示editText数据,但我无法在导航抽屉中的片段之间进行通信让您的活动来进行通信 //Put the value YourNewFragment ldf = new YourNewFragment (); Bundle args = new Bundle(); args.putString("

我试图在点击按钮时将数据从片段A发送到导航抽屉的片段B。我尝试了捆绑和意图,但两者都不起作用

在片段A中,我有editText和按钮,当我单击时,数据被传递到另一个片段


在片段B中有一个文本视图,其中将显示editText数据,但我无法在导航抽屉中的片段之间进行通信

让您的
活动来进行通信

//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("KEY", "VALUE");
ldf.setArguments(args);

//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
In onCreateView of the new Fragment:

//Retrieve the value
String value = getArguments().getString("KEY");
从控制导航抽屉的
片段的
位置,在
main活动
中获取一个
public static
变量。单击
FragmentA
中的按钮时,将
EditText
中的值存储到
MainActivity
public static
变量中。然后,当加载
FragmentB
时,检查
公共静态
值是否为空。如果不是
null
将该变量的值放置到所需位置

这不是一种在片段之间传递值的优雅方式,但在您的情况下,它会很好地工作

如果您正在寻找如何将值从一个片段传递到另一个片段,请尝试以下方法

// To pass some value from FragmentA
FragmentB mFragmentB = new FragmentB();
Bundle args = new Bundle();
args.putString("VALUE", value);
mFragmentB.setArguments(args);
并从
片段b
中使用代码获得传递的值

Bundle args = getArguments();
int value = args.getString("VALUE");

让您的
活动
进行沟通

从控制导航抽屉的
片段的
位置,在
main活动
中获取一个
public static
变量。单击
FragmentA
中的按钮时,将
EditText
中的值存储到
MainActivity
public static
变量中。然后,当加载
FragmentB
时,检查
公共静态
值是否为空。如果不是
null
将该变量的值放置到所需位置

这不是一种在片段之间传递值的优雅方式,但在您的情况下,它会很好地工作

如果您正在寻找如何将值从一个片段传递到另一个片段,请尝试以下方法

// To pass some value from FragmentA
FragmentB mFragmentB = new FragmentB();
Bundle args = new Bundle();
args.putString("VALUE", value);
mFragmentB.setArguments(args);
并从
片段b
中使用代码获得传递的值

Bundle args = getArguments();
int value = args.getString("VALUE");

1-创建应用程序类

public class MyApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
  }
 private static MyApplication mInstance;
  public static synchronized MyApplication getInstance() {
    return mInstance;
  }
  String mytext;

public String getMytext() {
    return mytext;
}

public void setMytext(String mytext) {
    this.mytext = mytext;
}
}
2-manifast中的应用程序名称标签

<application
    android:name=".MyApplication"
 .......
4-来自其他片段

MyApplication.getInstance().setMytext("your text here");
String text=MyApplication.getInstance().getMytext();

1-创建应用程序类

public class MyApplication extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    mInstance = this;
  }
 private static MyApplication mInstance;
  public static synchronized MyApplication getInstance() {
    return mInstance;
  }
  String mytext;

public String getMytext() {
    return mytext;
}

public void setMytext(String mytext) {
    this.mytext = mytext;
}
}
2-manifast中的应用程序名称标签

<application
    android:name=".MyApplication"
 .......
4-来自其他片段

MyApplication.getInstance().setMytext("your text here");
String text=MyApplication.getInstance().getMytext();

从第一个碎片启动碎片时

Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);

Fragment fragment = new SECONDFragment();

if (arguments != null) {
    fragment.setArguments(arguments);
}

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
SecondFragment

private String mData;

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

    if (getArguments() != null) {
        mData = getArguments().getString("key");
    }


}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.YourLayout, container, false);

    TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
    text.setText(mData);
    return rootView;
}

从第一个碎片启动碎片时

Bundle bundle = new Bundle();
bundle.putString("key", YOUR_EDITVIEW_TEXT);

Fragment fragment = new SECONDFragment();

if (arguments != null) {
    fragment.setArguments(arguments);
}

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.container, fragment);
ft.addToBackStack("");
ft.commit();
SecondFragment

private String mData;

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

    if (getArguments() != null) {
        mData = getArguments().getString("key");
    }


}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.YourLayout, container, false);

    TextView text = (TextView) rootView.findViewById(R.id.yourTextView);
    text.setText(mData);
    return rootView;
}

到目前为止你都试过了。发布你的代码。我认为你需要使用这个界面来检查youtube上的slidenerd在他的片段教程中你会得到一些东西发布你的代码和logcat。。。。。请参阅到目前为止尝试的堆栈溢出问题的答案。发布你的代码。我认为你需要使用这个界面来检查youtube上的slidenerd在他的片段教程中你会得到一些东西发布你的代码和logcat。。。。。请参阅有关堆栈溢出的答案