在两个以上的片段之间传递数据

时间:2017-11-04 09:26:02

标签: java android android-studio android-fragments fragment

我成功实现了使用接口将数据从一个片段传递到另一个片段。

MainActivity.java

MainActivity.java
public class MainActivity extends AppCompatActivity implements InputTab.SendMessage, FollowingTab.SendMessage, FollowerTab.SendMessage, ProfileTab.SendMessage, UserSearchResultTab.SendMessage {

    private SectionsPageAdapter pageAdapter;
    ViewPager viewPager;
    public static String currentUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, new ProfileTab());

        fragmentTransaction.commit();

        pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Sets up the ViewPager with the sections adapter

        viewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager((viewPager));

    }

    // Adds fragments to SectionsPageAdapter and gives names for the corresponding tab

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new InputTab(), "Search");
        adapter.addFragment(new ProfileTab(), "Profile");
        adapter.addFragment(new GithubTab(), "Github Repos");
        adapter.addFragment(new FollowerTab(), "Followers");
        adapter.addFragment(new FollowingTab(), "Followings");
        adapter.addFragment(new UserSearchResultTab(), "test");
        viewPager.setAdapter(adapter);
    }

    @Override
    public void sendDataToProfile(String message) {
        String tag = "android:switcher:" + R.id.container + ":" + 1;
        ProfileTab profile = (ProfileTab) getSupportFragmentManager().findFragmentByTag(tag);
        profile.displayReceivedData(message);
    }

    @Override
   public void sendDataToFollowers(String message) {
       String tag = "android:switcher:" + R.id.container + ":" + 3;
       FollowerTab follower = (FollowerTab) getSupportFragmentManager().findFragmentByTag(tag);
       follower.displayReceivedData(message);
   }

在InputTab.java中接口sendMessage

// Interface for passing-in data to a different tab(fragment)
interface SendMessage {
    void sendDataToProfile(String message);

    //void sendDataToSearchResult(String message);
    void sendDataToResult(String message);
    //void sendDataToFollowing(String message);
}

ProfileTab.java中的接收器

// This is the part where it handles the data passed in from another fragment (tab)
protected void displayReceivedData(String message)
{
    fetchData(message);
}

这样可行,但问题是它不适用于两个以上的片段,例如从A - >; B,C,D ......

我遵循了这个特定的教程:https://www.journaldev.com/14207/android-passing-data-between-fragments

所以在我的MainActivity.java上,sendDataToFollowers会给我一个错误,而sendDataToProfile工作正常。当我调试它时,FollowerTab跟随者为空。

什么是可能的解决方案,使其适用于两个以上的片段?

2 个答案:

答案 0 :(得分:0)

你可以试试这个,

标签0

getParent().getIntent().putExtra("key", "value"); 

标签1

String value = getParent().getIntent().getStringExtra("key");

另一种方式:

您必须将信息返回到您的片段活动中。并且您的活动将信息发送到其片段:

// In fragment A
((ParentActivity)getActivity()).dispatchInformations("test");

// In ParentActivity
public void dispatchInformations(String mesg){
    fragmentB.sendMessage(mesg);
}

答案 1 :(得分:0)

ViewPager's fragments之间传递数据的最佳方式是使用EventBus并将数据保存到Activity

  1. 使用EventBusLocalBroadCastRecevier立即将数据发送到有效fragments(通常一次有3个活动片段)。

    EventBust.getDefaut().sendData(YourDataObject);

  2. 在活动中保存数据,以便fragment处于有效状态时,只需从onViewCreated中的活动中提取已保存的数据:

    ((YOurActivity)getActivity()).getSavedData();