student1868 student1868 - 1 year ago 63
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

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:


import android.os.Bundle;
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 = "";
public static final String TAG = "CrimeFragment";

public void onCreate(@Nullable Bundle savedInstanceState) {

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

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(;
mTitleField.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

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


public void afterTextChanged(Editable s) {


mDateButton = (Button) v.findViewById(;

mSolvedCheckBox = (CheckBox) v.findViewById(;
mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean 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();

return fragment;

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

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

Answer Source

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