Android:从newInstance传递到onCreate

时间:2016-06-30 00:59:46

标签: java android android-fragments

我对Android编程很陌生,并试图弄清楚如何使用片段。我正在使用ViewPager在片段之间滚动。但是,当我滑动转到下一个片段时,数据不会更新。我一遍又一遍地得到同样的片段。

代码如下:

package com.bignerdranch.android.criminalintent;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import java.util.UUID;

/**
 * Created by nandeeka on 6/13/16.
 */
public class CrimeFragment extends Fragment {
    private Crime mCrime;
    private EditText mTitleField;
    private Button mDateButton;
    private CheckBox mSolvedCheckBox;
    public static final String EXTRA_CRIME_ID = "com.bignerdranch.android.criminalintent.crime_id";
    public static final String TAG = "CrimeFragment";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        UUID crimeID = (UUID) getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);
        Log.d(TAG, "onCreate: " + crimeID);
        mCrime = CrimeLab.get(getActivity()).getCrime(crimeID);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, container, false);

        mTitleField = (EditText) v.findViewById(R.id.crime_title);
        mTitleField.setText(mCrime.getTitle());
        mTitleField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                mCrime.setTitle(s.toString());
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mDateButton = (Button) v.findViewById(R.id.crime_date);
        mDateButton.setText(mCrime.getFormattedDate(getActivity().getApplicationContext()));
        mDateButton.setEnabled(false);

        mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
        mSolvedCheckBox.setChecked(mCrime.isSolved());
        mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCrime.setSolved(isChecked);
            }
        });

        return v;
    }

    public static CrimeFragment newInstance(UUID crimeID) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_CRIME_ID, crimeID);
        Log.d(TAG, "newInstance: " + crimeID);
        CrimeFragment fragment = new CrimeFragment();
        fragment.setArguments(args);

        return fragment;
    }
}

我想知道是否有人可以帮我弄清楚为什么当我从片段滑动到片段时,crimeID不会更新,这意味着片段中显示的信息保持不变。我知道正确的信息正在传递给newInstance,它在onCreate(Bundle)中似乎没有变化。

如果有人可以提供帮助,我会非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

您尝试从Intent获取仅与活动一起使用的数据。尝试使用getArguments获取您的UUID,如@Apporv所说。

为此,请更改以下代码:

UUID crimeID = (UUID) getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);

类似于:

// Get passing data from bundle
Bundle bundle = this.getArguments();
if (bundle != null) {
  UUID crimeID = (UUID) bundle.getSerializableExtra(EXTRA_CRIME_ID);
  // Do something with your CrimeID
  } else {
   Log.d(TAG, "UUID Data not found!");
}