omega
omega

Reputation: 43833

Android bundle value always reading 0

In my android app, I store a value 1 in a bundle, and then start the activity, then I read the bundle value from the new activity, and its 0. I'm not sure what's going wrong...

    content.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent myIntent = new Intent(context, ThreadScreen.class);
            myIntent.putExtra("thread_id", Integer.toString(thread.getId(), 10));
            context.startActivity(myIntent);

            Transition.TransitionForward(context);
        }
    });

the myIntent mExtras = Bundle[{thread_id=1}].

This code, puts a value 1 with key thread_id. Then I start the activity, and then I read it here

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_thread_screen);

    // activates the action bar
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    int thread_id = getIntent().getExtras().getInt("thread_id");
    setUpScreen(thread_id);

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

Here thread_id has a value of 0. Does anyone know what's wrong?

Thanks

Upvotes: 0

Views: 178

Answers (2)

Jukka Raanamo
Jukka Raanamo

Reputation: 451

You are writing a String key and reading an int. To write and read the same key, you need to use putExtra(String, int) and getInt.

Upvotes: 1

Vny Kumar
Vny Kumar

Reputation: 758

  1. getIntent().getExtras() will give you bundle
  2. In your case you have put String type value in Intent Extra
  3. So to get the value in ThreadClass Activity do below

    int thread_id = Integer.parseInt(getIntent().getStringExtra("thread_id"));

Hope this helps

Upvotes: 0

Related Questions