From debugging I can see that the extras are actually in the intent when startActivity(Intent)
is called. However, once getIntent()
is called in the new activity, the extras are gone. The intent's extras were fine before I added the AsyncTask
to handle the long computation.
Below is the code that starts the new activity RecipeListActivity
public class Fragment extends android.support.v4.app.Fragment {
private Entity mEntity;
private class RecipeOperation extends AsyncTask<Void, Void, ArrayList<Recipe>> {
Context mContext;
public RecipeOperation(Context context){
mContext = context;
}
@Override
protected void onPreExecute(){
// set gui visibilities
}
@Override
protected ArrayList<Recipe> doInBackground(Void... param) {
// long computation
}
@Override
protected void onPostExecute(ArrayList<Recipe> result) {
//start activity
Intent i = RecipeListActivity.newIntent(mContext, mEntity.getName(), result);
mContext.startActivity(i);
}
}
}
The RecipeOperation
task is executed like so
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new RecipeOperation(getActivity()).execute();
}
});
Below is the activity that I'm starting
public class RecipeListActivity extends SingleFragmentActivity {
private static final String EXTRA_NAME = "name";
private static final String RECIPES_TAG = "Recipes";
@Override
protected Fragment createFragment(){
String Name = getIntent().getExtras().getString(EXTRA_NAME);
ArrayList<Recipe> recipes = getIntent().getExtras().getParcelableArrayList(RECIPES_TAG);
return RecipeListFragment.newInstance(Name,recipes);
}
public static Intent newIntent(Context packageContent, String Name, ArrayList<Recipe> recipes){
Intent intent = new Intent(packageContent,RecipeListActivity.class);
intent.putExtra(EXTRA_NAME, Name);
intent.putParcelableArrayListExtra(RECIPES_TAG,recipes);
return intent;
}
}
Here is the superclass for RecipeListActivity
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@LayoutRes
protected int getLayoutResId(){
return R.layout.activity_masterdetail;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
What happened to the intent's extras upon starting the new activity and how can I get the extras to persist?
It turns out the intent's extras were null because there was an error in my Recipe
class which implements Parcelable
. Essentially, my parcel.write
commands did not match with my parcel.read
commands. So if anyone else has this problem check your parcelables. Putting an incorrectly implemented parcelable into an intent will nullify all extras by the time the next activity starts and getIntent
is called.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.