【问题标题】:How to send data from fragment to activity [duplicate]如何将数据从片段发送到活动[重复]
【发布时间】:2017-08-01 05:28:06
【问题描述】:

在我的应用程序中,我在一个活动中有两个片段。 在其中一个片段中,我有数据,例如:

String name = "Transporter";

我想将此名称发送到容器活动。 我该怎么做?请帮帮我。

【问题讨论】:

  • 你试过什么?
  • 利用接口:)
  • 查看 Amit Sharma 的答案

标签: android android-fragments android-activity


【解决方案1】:

片段将附加到您从中启动的活动。

因此,您可以在活动中创建一个回调方法,该方法可以使用活动上下文对象从片段中调用。

请看下面的代码sn-p:

public class YourFragment extends Fragment{

       OnCallbackReceived mCallback;

// Implement this interface in your Activity.

public interface OnCallbackReceived {
    public void Update();
}

在您的片段中:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (OnCallbackReceived) activity;
    } catch (ClassCastException e) {

    }
}

    // You can Call the event from fragment as mentioned below
    // mCallback is the activity context. 
    mCallback.Update();

活动:

public class MainActivity extends Activity
        implements YourFragment.OnCallbackReceived {

    // Implemented method.
    public override void Update() {
        // Write your logic here.
    }

【讨论】:

    【解决方案2】:

    如果你从一个片段调用一个活动并且你想向活动发送数据,你可以使用如下的意图:

    Intent intent = new Intent(getActivity(), YourActivity.class);
    String name = "Transporter";
    intent.putExtra("name", name);
    startActivity(intent);
    

    在你的活动中,你应该得到这样的数据:

    try {
    
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
    
    } catch(Exception e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 我不使用 startActivity(intent);
    【解决方案3】:

    试试下面的代码

    // You Activity implements your interface
    public class YourActivity implements FragmentA.TextClicked{
        @Override
        public void sendText(String text){
            // Your text 
        }
    }
    

    // Fragment A定义了一个接口,需要的时候调用方法

    public class FragmentA extends Fragment {
    
        TextClicked mCallback;
    
        public interface TextClicked {
            public void sendText(String text);
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
    
            // This makes sure that the container activity has implemented
            // the callback interface. If not, it throws an exception
            try {
                mCallback = (TextClicked) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                    + " must implement TextClicked");
            }
        }
    
        public void someMethod(){
            mCallback.sendText("YOUR TEXT");
        }
    
        @Override
        public void onDetach() {
            mCallback = null; // => avoid leaking, thanks @Deepscorn
            super.onDetach();
        }
    }
    

    【讨论】:

      【解决方案4】:

      你需要创建一个接口。

      Like 
      
      interface DataReciver
      {
      
        void getData(String data);
      
      }
      
      
      public class MainActivity extends AppcompactActivity
      {
      
       // On fragment load
      
      
      oncreate
      {
        DataReciver obj=new DataReciver()
        {
         @overide
         void getData(String data)
         {
            // Here is your data in data variable 
      
         }
        }
      
      
        frgamenttranstatiion.add(YOUR CONTAINER, new FRAGMENT1(obj));
      }
      
      
      }
      
      
      Create a fragment with constructor
      
      public class Fragment1
      {
      
      
       Fragment1(DataReciver datareciver)
       {
         datareciver.getData("amit sharma");
       }
      }
      

      【讨论】:

      • 我的朋友我想从片段发送到活动,而不是活动到片段
      • 仔细检查代码这是将数据从片段发送到活动当我调用 datareciver.getData("amit sharma");这转到活动 obj,即活动
      【解决方案5】:

      目前最好的方法(和我的建议)是使用ViewModel。您可以在 android 网站上找到示例。但我应该说它仍然是测试版。

      https://developer.android.com/topic/libraries/architecture/viewmodel.html

      【讨论】:

        【解决方案6】:

        你可以这样做

        在像这样的活动中创建方法

        public void doSomething(String abc)
        {
          // do your stuff here
        }
        

        并从片段访问它

        喜欢

        ((HomeActivity)getActivity()).doSomething(string);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-27
          • 2020-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-19
          • 2016-12-23
          • 1970-01-01
          相关资源
          最近更新 更多