Where/How to Getintent().Getextras() in an Android Fragment

Where/How to getIntent().getExtras() in an Android Fragment?

What I tend to do, and I believe this is what Google intended for developers to do too, is to still get the extras from an Intent in an Activity and then pass any extra data to fragments by instantiating them with arguments.

There's actually an example on the Android dev blog that illustrates this concept, and you'll see this in several of the API demos too. Although this specific example is given for API 3.0+ fragments, the same flow applies when using FragmentActivity and Fragment from the support library.

You first retrieve the intent extras as usual in your activity and pass them on as arguments to the fragment:

public static class DetailsActivity extends FragmentActivity {

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

// (omitted some other stuff)

if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(
android.R.id.content, details).commit();
}
}
}

In stead of directly invoking the constructor, it's probably easier to use a static method that plugs the arguments into the fragment for you. Such a method is often called newInstance in the examples given by Google. There actually is a newInstance method in DetailsFragment, so I'm unsure why it isn't used in the snippet above...

Anyways, all extras provided as argument upon creating the fragment, will be available by calling getArguments(). Since this returns a Bundle, its usage is similar to that of the extras in an Activity.

public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();

// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);

return f;
}

public int getShownIndex() {
return getArguments().getInt("index", 0);
}

// (other stuff omitted)

}

Android - getIntent() from a Fragment

You can use a getIntent() with Fragments but you need to call getActivity() first. Something like getActivity().getIntent().getExtras().getString("image") could work.

Send/get intent extras in fragment class

Theres three different scenerios for obtaining a Bundle.

  1. If you want to use Bundle arguments to initialize your fragment, you have to use myfragment.setArguments(mybundle); when you first create your fragment.

  2. If you want your fragment to use the intent that was passed to the activity you can call:

    getActivity().getIntent()

  3. If you want only your fragment to intercept an intent then you have to register your fragment using the LocalBroadcastManager.
    You can see here on how to create this setup: how to use LocalBroadcastManager?

Get Activity's 1 Intent extras from Activity's 2 static fragment

Following @Gabe Sechan's proposal, I used the following way to pass the bundle to RecipeStepsFragment from RecipeStepsActivity.

1) I receive the intent extras from SelectRecipeActivity to RecipeStepsActivity's onCreate method.

2) In RecipeStepsActivity's onCreate method I get a reference to RecipeStepsFragment by calling findFragmentById like this:

RecipeStepsFragment stepsFragment = (RecipeStepsFragment)getSupportFragmentManager()
.findFragmentById(R.id.master_steps_fragment);

3) Then I get the intent extras creating a Bundle, which I then pass as RecipeStepsFragment's arguments, like this:

Bundle args = getIntent().getExtras();
//Pass the intent extras to the fragment using a bundle
if (args != null) {
//show Dessert Name in Toolbar
mRecipe = args.getParcelable(EXTRAS_RECIPE_ITEM);
assert mRecipe != null;
setTitle(mRecipe.getName());

assert stepsFragment != null;
stepsFragment.setArguments(args);
}

4) Now in RecipeStepsFragment's ->onActivityCreated<- method (to be sure that the hosting activity has been created and so we have got the intent extras from the previous activity), I simply get the step's 3 arguments like this:

Bundle fragmentArgs = getArguments();

which contains the same extras SelectRecipeActivity passed to the RecipeStepsActivity.

What is meant by getIntent().getExtras()?

getIntent().getExtras() is used to get values from intent that are stored in bundle.
Intent class is used to switch between activities. But sometimes we need to send data from one activity to another. So, at this particular moment we need to set some values to intent that can be transferred to destination activity. We can achieve this by the following code -

Bundle bundle = new Bundle();
bundle.putString("key1","someValue");
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putStringExtra("key","value");
intent.putExtras(bundle);
startActivity(intent);

Now, in the second activity we can get the value of "key" so we can use that in second activity. To do so, we use the getIntent().getIntent can store a Bundle. Let's see an example -

Intent intent=getIntent();
Bundle valueFromFirstActivity = intent.getExtras();
String valueOfKey = intent.getStringExtra("key");
String valueOfKey = bundle.getString("key1");

So this way, one can get values from activities. Bundle is a class that can hold values within itself and that instance of bundle can be given to intent using putExtras(). It is quite helpful in transferring the custom array list.



Related Topics



Leave a reply



Submit