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

Through exercises, I used this:

In action 1:

Intent i = new Intent(getApplicationContext(), MyFragmentActivity.class); i.putExtra("name", items.get(arg2)); i.putExtra("category", Category); startActivity(i); 

In action 2:

 Item = getIntent().getExtras().getString("name"); 

How do you do this using fragments? I also use the v4 compatibility library.

Does this happen in FragmentActivity? Or the actual fragment? And what method does this come in? OnCreate? onCreateView? other?

And can I see a sample code, please?

EDIT: It's worth noting that I'm trying to save Activity 1 as an Activity (or actually ListActivity, where I pass the listitem intent on click), and then go to a set of tabbed snippets (via Fragment Activity), and I need either a tab to get additional services. (Hope this is possible?)

+82
android android-intent android-activity android-fragments
Jul 09 2018-12-12T00:
source share
2 answers

What I usually do, and I believe that this is what Google intended for developers, is also to still get extra functions from Intent to Activity , and then pass any extra data to fragments, creating them with arguments.

Actually an example on the Android dev blog that illustrates this concept, and you'll see this in a few API examples. Although this specific example is for API 3.0+ fragments, the same thread applies when using FragmentActivity and Fragment from the support library.

First, you extract the presets of intent as usual in your activity and pass them 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(); } } } 

Instead of calling the constructor directly, it might be easier to use a static method that connects the arguments to the fragment for you. This method is often called newInstance in the examples provided by Google . Actually there is a newInstance method in DetailsFragment , so I'm not sure why it is not used in the snippet above ...

In any case, all additional functions provided as an argument when creating the fragment will be available by calling getArguments() . Since this returns a Bundle , its use is similar to the use of additional functions in 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) } 
+97
Jul 9 2018-12-12T00: 00Z
source share

you can still use

 String Item = getIntent().getExtras().getString("name"); 

in fragment , you just need to call getActivity() :

 String Item = getActivity().getIntent().getExtras().getString("name"); 

This saves you from writing code.

+149
Feb 07 '13 at 8:31
source share



All Articles