【问题标题】:How to send a Parcelable object to a DialogFragment?如何将 Parcelable 对象发送到 DialogFragment?
【发布时间】:2012-03-07 22:59:25
【问题描述】:

我可以将我的数据从 Activity1 发送到 Activity2 与典型..

Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("state", getIntent().getParcelableExtra("state"));
intent.putExtra("schools", temp);
startActivity(intent);

一旦我在Activity2,它就可以正常工作,问题是如何使它从Activity1 工作到DialogFragment?一旦我对DialogFragment 进行编码,您如何发送和检索可包裹的对象?有什么可用的例子,你可以指点我吗?

【问题讨论】:

    标签: android dialog fragment parcelable


    【解决方案1】:

    我认为this 可以提供帮助。
    基本上是在对话框的onCreate() 中使用setArguments() 和后来的getArguments()

    【讨论】:

      【解决方案2】:

      使用 Bundle 将 Parcelable 对象传递给您的 DialogFragment。

      http://developer.android.com/reference/android/os/Bundle.html

      (编辑:)

      假设您在 Activity1 的某处需要一个 TimePicker DialogFragment:

      // This is a static inner class which resides inside your Activity1
      // so you will face this limitation :
      // You can only access static method from inside this. You also can
      // not remove static keyword or you will face memory leak.
      
      public static class StartTimePickerFragment extends DialogFragment
              implements TimePickerDialog.OnTimeSetListener {
      
      static StartTimePickerFragment newInstance (int arg, YourParcelableObj obj) {
      
              StartTimePickerFragment DialogFrag = new 
                  StartTimePickerFragment();
      
              Bundle args = new Bundle();
              args.putInt("Whatever", arg);
              // for Parcelable :
              args.putParcelable ("Whatever2", obj);
              DialogFrag.setArguments(args);
              return DialogFrag;
          }
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mNum = getArguments().getInt("whatever");
      }
      
       public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
      
                  // now here you can not directly access class methods and
                  // and data members. so get the Activity object and then
                  // you are good to 'set/get' them here in  your
                  // DialogFragment
      
                  Activity1 activity = (Activity1) getActivity();
                  activity.your_non_static_method(hourOfDay, minute);
                  activity.your_non_static_activitidy_member_Data = "whatever";
          }
      }
      

      【讨论】:

      • 请在链接周围提供一些内容。换句话说,提供问题的答案,仅使用链接作为参考,以便对您的答案进行更全面的处理。在 SO 上这样做的原因是为了以防万一链接变坏。如果在这个答案的情况下发生这种情况,那么未来的 SO 用户将无事可做。
      猜你喜欢
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      相关资源
      最近更新 更多