As the title suggest.
I've downloaded Fragment
code from here, http://developer.android.com/shareables/training/FragmentBasics.zip.
It is Fragment example from Android Official Developer site. http://developer.android.com/training/basics/fragments/fragment-ui.html
This is the MainActivity.java
's onCreate()
:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create an instance of ExampleFragment
HeadlinesFragment fragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an Intent,
// pass the Intent's extras to the fragment as arguments
//fragment.setArguments(getIntent().getExtras());
Bundle args= new Bundle();
args.putString("category", "clothes");
args.putString("item", "shirts");
fragment.setArguments(args);
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment).commit();
}
}
And HeadlinesFragment.java
's onCreate()
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We need to use a different list item layout for devices older than Honeycomb
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
Bundle args = getArguments();
if (args == null) {
Toast.makeText(getActivity(), "arguments is null " , Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "text " + args , Toast.LENGTH_LONG).show();
}
// Create an array adapter for the list view, using the Ipsum headlines array
setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines));
}
I've read several QA here, like this one Fragment getArguments() returns null, and many other that related to setArguments()
and getArguments()
, but still I'm stuck.
And I've moved the Bundle
and Toast
code to onAttach()
and to onCreateView()
with no avail. What wrong with the my code? I think I'm missing something, but dunno what is it.
Please Help! Thanks.
Edit:
I'll state my intention more clearly. In FragmentBasic that I downloaded, there's MainActivity.java, HeadlinesFragment.java, and ArticlesFragment.java. The 'communication' from MainActivity.java to ArticlesFragment.java is not the problem here. What I want is to send data from MainActivity.java to HeadlinesFragment.java. Their connection's like this:
--------------------------------------
| MainActivity <-> HeadlinesFragment |
| | |
| |>> ArticlesFragment |
--------------------------------------
And HeadlinesFragment
is running at Runtime.
*These code works, when using Android gadget with < 600px width. But doesn't work when using on tablet ( >= 600px), As proved by @Tesla1984 below. But what I want is same result either on gadget < 600px and on gadget > 600px.
It looks like you are inserting a key and value pair into your bundle. You probably need to reference the key value as in
getArguments().getString(category);
According to the docs for putString: Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key. Either key or value may be null.
Parameters key a String, or null value a String, or null
I had the same problem, but solved it :)
My problem was that I had the
<fragment android:name="">
element in the Activity's XML layout. Therefore the onCreate() of the Fragment was called before the calls in Java code, thus not setting the arguments.I removed the
<fragment>
element from my XML layout and it worked!I've solved it. Looks like the only way to send data from MainActivity.java to HeadlinesFragment.java is from callbacks (If anybody else know other ways, please contribute, then we have some other ways beside this one, helping others with this kind of problem).
The main code from is MainActivity.java's function
public Bundle getBundle() {}
, then set theinterface
section on HeadlinesFragment.java, and addpublic Bundle getBundle();
, and last, call it from HeadlinesFragment.java'sonCreate
.What confuse me is
fragment.setArguments(getIntent().getExtras());
on MainActivity.java'sonCreate
. They put that code there, and I believing it will works cause it's from Android Official Developer Guide and API http://developer.android.com/training/basics/fragments/fragment-ui.html, but it didn't work (now I believe that piece of code won't do anything). So, anyone who read the tutorial or sample from there, take it with a grain of salt!Codes below, so everyone can understand it.
MainActivity.java:
HeadlinesFragment.java:
@tonny
i've download the FragmentBasics.zip. i only change the argument name.here is the code and result pic.
MainActivity
HeadlinesFragment
here is the result