Trần Đức Tâm
Trần Đức Tâm

Reputation: 4443

Bundle getInt only return default value

I have a problem that is this code only return default value is -1. I tried to debug, it has value so i don't know why it alway return -1.

private static final String KEY_CATEGORY_ID = "category";
Bundle bundle = getArguments();
mCategoryId = bundle.getInt(KEY_CATEGORY_ID, -1);

This is my debug value:

bundle  Bundle  (id=830037735464)
Bundle[{category=2}]

Upvotes: 0

Views: 2061

Answers (1)

Usman Afzal
Usman Afzal

Reputation: 558

In your activity setArguments in this way

mFragment = new MyFragment();
Bundle extras = this.getIntent().getExtras();
extras.putInt("category", 10);
mFragment.setArguments(extras);
mFragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragmentTransaction.add(R.id.profile_fragment, mFragment);
mFragmentTransaction.commit();

In fragment get it in this way

Bundle bundle = getArguments();
catgory = bundle.getInt("category");

Upvotes: 2

Related Questions