student1868 student1868 - 5 months ago 13
Java Question

Android: Passing Extra From newInstance to onCreate

I am pretty new to Android programming and and am trying to figure out how to use fragments. I am using a

ViewPager
to scroll between fragments. However, when I swipe to go to the next fragment, the data does not update. I get the same fragment over and over again.

The code is as follows:

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;
}
}


I was wondering if someone could help me figure out why as I swipe from fragment to fragment, the
crimeID
does not update, meaning that the information displayed in the fragment stays the same. I know that the correct information is being passed to
newInstance
, it just does not seem to be changing in
onCreate(Bundle)
.

I would really appreciate it if someone could help. Thanks!

Answer

You trying to get the data from Intent which only working with activity. Try getting your UUID with getArguments as @Apporv says.

To do that change the following code:

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

to something like:

// 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!");
}
Comments