启动新活动后,Intent extras为null

时间:2017-08-29 01:33:20

标签: android android-intent

从调试中我可以看到,在调用const data = { "address": "6210 place", "name": "frank", "paymentId": "ch_1AjJRVDfJ ryYeuMpJC80cp5k", "email": "g@mail.com", "cart": [ { "items": { "59752 28a215c0f074b64f58e": { "item": { "_id": "5975228a215c0f074b64f58e", "title": "Bracelet 3", "imagePath": "https:// www.costco.com/wcsstore/CostcoUSBCCatalogAssetStore/category-tiles/pearl-bracelets.jpg", "description": "This is brace let 3", "price": 12, "__v": 0 }, "qty": 1, "price": 12 }, "59752242215c0f074b64f58c": { "item": { "_id": "597522 42215c0f074b64f58c", "title": "Bracelet 1", "imagePath": "https://img0.etsystatic.com/160/0/12655872/il_340x270.11871 91078_i2ha.jpg", "description": "This is bracelet 1", "price": 10, "__v": 0 }, "qty": 2, "price": 20 }, "5975226a2 15c0f074b64f58d": { "item": { "_id": "5975226a215c0f074b64f58d", "title": "Bracelet 2", "imagePath": "http://media .tiffany.com/is/image/Tiffany/EcomBrowseM/paloma-picasso-knot-bead-bracelet-34946183_963148_ED.jpg?op_usm=1.00,1.00,6.0 0&defaultImage=NoImageAvailable&&", "description": "This is bracelet 2", "price": 5, "__v": 0 }, "qty": 1, "price": 5 } }, "totalQty": 4, "totalPrice": 37 } ], "__v": 0 }; console.log('looping through Object.keys') Object.keys(data.cart[0].items).forEach(key => { const cartItem = data.cart[0].items[key]; console.log('cartItem.item.imagePath', cartItem.item.imagePath) }) console.log('looping through Object.values') Object.values(data.cart[0].items).forEach(cartItem=> { console.log('cartItem.item.imagePath', cartItem.item.imagePath) })时,额外内容实际上在意图中。但是,在新活动中调用startActivity(Intent)后,额外内容就会消失。在我添加getIntent()来处理长计算之前,意图的附加功能很好。

以下是启动新活动AsyncTask

的代码
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); } } 任务执行如此

RecipeOperation

以下是我正在开展的活动

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new RecipeOperation(getActivity()).execute();
        }
    });

}

这是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; }

的超类
RecipeListActivity

}

在开始新活动时,意图的额外内容发生了什么?如何才能使附加内容持续存在?

1 个答案:

答案 0 :(得分:0)

事实证明,意图的附加内容为空,因为我的Recipe类中有一个实现Parcelable的错误。基本上,我的parcel.write命令与我的parcel.read命令不匹配。 因此,如果其他人有此问题,请检查您的parcelables。将错误实现的parcelable放入intent中会在下一个活动开始时调用所有额外内容并调用getIntent

相关问题