问题
I am trying to pass an intent with data from Activity A to FragmentActivity B to Fragment B. A button from Activity A starts FragmentActivity B. The "year" string sets a filter with which I query a mysql database on a remote server. The code below works but only half the time.
I have no idea what causes it to filter just some times and at other times it just returns the whole table unfiltered. I am guessing it has to be related to the intent I am using in the fragment because the filter worked without issue in the version of the app without the fragment. It also seems inefficient the way I am sending the intent from Activity A to FragmentActivity B to Fragment B. How can I fix this problem?
Activity A:
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(FindMovie.this, ShowMovies.class);
intent.putExtra("year", year1);
startActivity(intent);
}
});
FragmentActivity B:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_movies);
if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(android.R.id.content, new ImageGridFragment(), TAG);
ft.commit();
}
Intent i = getIntent();
year = i.getStringExtra("year");
//resend through another intent to the fragment B
Intent intent = new Intent(getApplicationContext(), ImageGridFragment.class);
intent.putExtra("year", year);
}
Fragment B:
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
Bundle extras = getActivity().getIntent().getExtras();
year = extras.getString("year");
return v;
}
回答1:
I think this is a thread timing issue, your FragmentActivityB is creating the fragment, which is sent to a thread (I would think) and then you are adding the new value from the original thread, and sometimes the second thread is fast enough to execute FragmentB onCreateView
before it gets the new value. What you can do is add the value of 'year'
during the FragmentTransaction.
@Override
protected void onCreate(Bundle savedInstanceState) {
// Create a new fragment and bundle
Fragment fragment = new ImageGridFragment();
Bundle bundle = new Bundle();
// Put variables in bundle and add to fragment
bundle.putString("year", getIntent().getStringExtra("year"));
fragment.setArguments(bundle);
// Insert the fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.add(android.R.id.content, fragment)
.commit();
}
In FragmentB get the 'year'
like this:
year = getArguments().getString("year");
I think this would solve the issue, let me know what happens though,
Cheers.
来源:https://stackoverflow.com/questions/17226251/intent-within-fragment-works-only-half-the-time