【问题标题】:how to pass a value using intents and bundle如何使用意图和捆绑传递值
【发布时间】:2017-03-11 06:05:50
【问题描述】:

我试图将一个值从我的Activity 传递给我的Fragment,但Bundle 始终为空。

活动

CallLogsFragment callLogfrag = new CallLogsFragment();
Intent intent = new Intent(this, DeviceUsageActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", callgroup);
callLogfrag.setArguments(bundle);
this.startActivity(intent);

用于从Activity检索值的片段

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    bundle = this.getArguments();
    if (bundle != null) {
        int myInt = bundle.getInt("key", 0);
    }
    View view = inflater.inflate(R.layout.fragment_call_logs, container, false);
    load(view);
    setupList(view);

    return view;
}

【问题讨论】:

  • 那么显然 onCreateView 没有在他在这里设置的片段中被调用(也许是来自活动布局的片段?)
  • DeviceUsageActivity.class 是活动或片段
  • @zidane 你能回复一下吗,如果你正在开始一个活动,那么你应该把意图附加内容放在你的活动中,并使用 getIntent().getExtras.get(key)
  • 你为什么要this.startActivity(intent)?这将启动一个新的 Activity。你不想附加一个片段吗?

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


【解决方案1】:

试试这个

 // send value from activity to fragment 

CallLogsFragment callLogfrag = new CallLogsFragment();
Bundle bundle = new Bundle();
bundle.putInt("key", "4");
callLogfrag.setArguments(bundle);
 // call fragment from activity
getSupportFragmentManager().beginTransaction().replace(R.id.containar, callLogfrag).commit();

//  get value in fragment 

    if (getArguments() != null) {
        int status = getArguments().getInt("key");

【讨论】:

    【解决方案2】:

    如果我对你的问题很清楚!!您正在尝试将数据从一个活动发送到另一个活动中的片段。

    然后您需要首先在 Intent 中发送数据以启动第二个活动。然后从意图中获取数据并将您的数据设置为片段作为参数并将片段添加到父活动。

    //here you go, from first activity
    Intent intent = new Intent(this, DeviceUsageActivity.class);
     Bundle bundle = new Bundle();
    bundle.putInt("key", callgroup);
    intent.putExtra("yourdata",bundle);
    //start your activity - parent activity of your fragment
    this.startActivity(intent);
    
    //then get your data in DeviceUsageActivity in onCreate()
     Bundle yourdata = null;
    if(getIntent().getExtras() != null) {
           yourdata = getIntent().getBundleExtra("yourdata");
    }
    
    //Then sent data to fragment
    CallLogsFragment callLogfrag = new CallLogsFragment();
    
    callLogfrag.setArguments(yourdata);
     // add fragment to parent activity
    getSupportFragmentManager().beginTransaction().replace(R.id.containar, callLogfrag).commit();
    
    //rest you know - get bundle argument in fragmet
    

    【讨论】:

      【解决方案3】:

      从Activity到存储数据

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

      要将数据检索回片段,如果仅在片段中需要,请使用 Context 作为 getActivity

      Bundle extras = getIntent().getExtras();
      if (extras != null) {
          String value = extras.getString("key");
          //The key argument here must match that used in the other activity
      }
      

      【讨论】:

      • OP 想要将数据从 Activity 发送到 Fragment。不是 Activity 到另一个 Activity
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      相关资源
      最近更新 更多