Android 如何通过活动将额外数据从一个片段传输到另一个片段

Android 如何通过活动将额外数据从一个片段传输到另一个片段,android,android-layout,android-emulator,android-intent,Android,Android Layout,Android Emulator,Android Intent,例如,在由ListActivity主持的列表视图中,当用户单击列表上的项目时,将启动一个新活动,并且以前的活动将额外数据传输到新活动,如下所示: public class Notepadv2 extends ListActivity { ... @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick

例如,在由ListActivity主持的列表视图中,当用户单击列表上的项目时,将启动一个新活动,并且以前的活动将额外数据传输到新活动,如下所示:

public class Notepadv2 extends ListActivity {
    ...

   @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
     }
}
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
如果我使用碎片,应该是什么样子?我的意思是,如果我有一个活动承载2个片段,并进行如下的片段事务:

public class Notepadv2 extends ListActivity {
    ...

   @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
     }
}
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
如何通过宿主活动将额外数据从一个片段传输到另一个片段

我知道在android开发者的网页上,有一个关于如何使用片段以及如何与活动进行通信的说明,但没有关于如何将数据从一个片段传输到另一个片段的说明。…

使用

Bundle data = new Bundle();
data.putString("name",value);
Fragment fragment = new nameOfFragment();
fragment.setArguments(data);
.navigateTo(fragment);

从“活动”发送数据,目的是:

Bundle bundle = new Bundle();
bundle.putString("key", "value");
// set Fragmentclass Arguments
Fragmentclass fragmentobj = new Fragmentclass();
fragmentobj.setArguments(bundle);
在Fragment onCreateView方法中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("key");    
    return inflater.inflate(R.layout.fragment, container, false);
}

我希望这对您有帮助。

您的意思是我不需要在fragment中定义接口并让主机活动实现接口,该接口在fragment和主机acvity之间建立通信。。。相反,我可以使用您的代码直接导航到下一个片段?@它不是空的,请您进一步说明;android studio似乎没有识别出这个导航到是我的本地方法。可能是的重复