Start of the form of activity Fragment

I searched the site and there were similar questions like mine, but none of the topics were my answer

look at this image:

enter image description here

so it is clear that I want to start CrimeActivity

by sending intent

from CrimeListFragment

+ an extra

to myintent

the book I read for Android programming, the author said:

Launching an activity from a fragment works in much the same way as launching an activity from another activity. You call the Fragment.startActivity (Intent) method, which calls the corresponding activity method behind the scenes

CrimeListFragment.java:

public void onListItemClick(ListView l, View v, int position, long id) {
   // Get the Crime from the adapter
   Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);

   // Start CrimeActivity
   Intent i = new Intent(getActivity(), CrimeActivity.class);
   i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId());
   startActivity(i);
}

      

the second part now extracts intent

that too extra

, and the author said about it:

There are two ways a fragment can access data in its intent: a simple, direct shortcut, and a complex, flexible implementation. First, you are going to try a shortcut. Then you have a comprehensive and flexible solution that includes fragmentary arguments.

and my problem is with the first way, shortcut

In the shortcut, CrimeFragment will simply use the getActivity () method to access CrimeActivitys directly. Go back to CrimeFragment and add a key for additional. Then in onCreate (Bundle), extract the extra from the CrimeActivitys intent and use it to get the crime

CrimeFragment.java:

public class CrimeFragment extends Fragment {
  public static final String EXTRA_CRIME_ID =
    "com.bignerdranch.android.criminalintent.crime_id";
  private Crime mCrime;
  ...
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCrime = new Crime();
    UUID crimeId = (UUID)getActivity().getIntent()
      .getSerializableExtra(EXTRA_CRIME_ID);

    mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}

      

Lack of direct search

With access to the fragment, the intent that belongs to the hosting activity makes the code simple. However, this requires encapsulating your fragment. CrimeFragment is no longer reused as it expects it to always be allocated as a result of an action whose intent is specified by the alternate name EXTRA_CRIME_ID.

This might be a reasonable expectation in the CrimeFragments part, but it means that CrimeFragment, as currently written, cannot be used with just any activity.

My question and problem is the last suggestion, why CrimeFragment

can't this snippet ( ) be used with just anyone Activity

???

+3


source to share


2 answers


The author explains this. Yours CrimeFragment

in your method onCreate()

gets its hosting activity (via getActivity()

) and then tries to get the UUID from the Intent

one used to start Activity

.

This means that any action containing yours CrimeFragment

must now obey this rule, i.e. its intent must have (in it) the additional meaning specified by the name EXTRA_CRIME_ID

. If this action fails, you will see an exception that will be thrown in CrimeFragment

onCreate()

.



Try to create this snippet in a new activity of your own creation to see what happens.

+3


source


search in onActivityCreated ()



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        ....
    }
    else {
        UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);
    }
}

      

0


source







All Articles