简体   繁体   English

从另一个Activity中的Activity传递片段中的数据

[英]Passing data from fragment in an Activity from Another Activity

I have researched on passing data from Activity to Activity. 我研究过将数据从Activity传递给Activity。 But I could not use the same method to pass to my fragment. 但我无法使用相同的方法传递给我的片段。 How do I pass the data to my fragment? 如何将数据传递给我的片段?

The Code I am using is 我正在使用的准则是

    Intent i = new Intent(getApplicationContext(), NewActivity.class);
    i.putExtra("new_variable_name","value");
    startActivity(i);

and In the other activity 在另一项活动中

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("new_variable_name");
    }

Its pretty easy to pass data between two fragments with the help of interface. 在界面的帮助下,很容易在两个片段之间传递数据。

I think this is what you are looking for . 我想就是你要找的东西。

Hope this helps :) 希望这可以帮助 :)

To pass the date from activity to fragment,it is nicely explained here . 要将日期从活动传递到片段, 这里很好地解释

  1. Use setArguments() to pass a Bundle to a Fragment. 使用setArguments()Bundle传递给Fragment。 Fragment will retain this bundle even on configuration change. 即使在配置更改时,Fragment也会保留此捆绑包。 It will be delivered to onCreate() and other methods of fragment. 它将被传递给onCreate()和其他片段方法。

  2. Find an existing fragment by its id or tag: getFragmentManager.findFragmentById() , and call fragment class's method. 通过其id或标记查找现有片段: getFragmentManager.findFragmentById() ,并调用片段类的方法。

  3. Save parent activity reference to a local variable in onAttach() of fragment and call parent activity class's methods from fragment. 将父活动引用保存在片段的onAttach()中的局部变量中,并从片段调用父活动类的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM