如何在 FragmentTransaction 中添加 Extra Android SDK 21

How to putExtra in FragmentTransaction Android SDK 21

我已将我的应用程序从 activity 视图转换为片段,但我不知道如何将额外信息传递到我的新片段。在这里,我从另一个片段创建了一个新片段。

这是旧的工作代码

           Launch a new Activity to display the selected sensor
           Intent intent = new Intent(getActivity(), SensorViewFragment.class);

           // Push the sensor index to the new Activity
           intent.putExtra(SensorViewFragment.SENSOR_INDEX_EXTRA, position);

           // Start the activity
           startActivity(intent);*/

这是新代码,我不知道如何在其中添加 Extra

    // Set a listener to respond to list item clicks
    sensorListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // Create new fragment and transaction
            Fragment newFragment = new SensorViewFragment();
            // consider using Java coding conventions (upper first char class names!!!)
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.container, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();
        }
    });

推荐的解决方案是使用参数 Bundle。这样一来,数据和旧的附加功能一样,会在配置更改时自动保留在最近的任务列表中,等等。

Google的典型做法是在片段class上使用工厂方法(newInstance())来处理将数据打包到参数Bundle.

例如,在 this sample app 中,我正在为 ViewPager 创建片段。我想将 position (页码)传递到片段中,因此片段可以在 UI.

中使用它

在我的片段 class (EditorFragment) 上,我有一个 newInstance() 工厂方法,它将提供的 position 放入参数 Bundle 中,并且我通过 getArguments():

onCreateView() 方法中使用该值
/***
  Copyright (c) 2012-14 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.pager;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class EditorFragment extends Fragment {
  private static final String KEY_POSITION="position";

  static EditorFragment newInstance(int position) {
    EditorFragment frag=new EditorFragment();
    Bundle args=new Bundle();

    args.putInt(KEY_POSITION, position);
    frag.setArguments(args);

    return(frag);
  }

  @Override
  public View onCreateView(LayoutInflater inflater,
                           ViewGroup container,
                           Bundle savedInstanceState) {
    View result=inflater.inflate(R.layout.editor, container, false);
    EditText editor=(EditText)result.findViewById(R.id.editor);
    int position=getArguments().getInt(KEY_POSITION, -1);

    editor.setHint(String.format(getString(R.string.hint), position + 1));

    return(result);
  }
}

当我想创建片段的实例时,我只是使用工厂方法(EditorFragment.newInstance(position))。