0

I have two activities, MainActivity and SecondActivity. While passing int value from MainActivity to SecondActivity it becomes "0". I have tried with and without bundle, tried various solution already present on StackOverFlow, but no go.

Here is my code: MainActivity

final Intent intent = new Intent(MainActivity.this, SecondActivity.class);

    final TextView textView = findViewById(R.id.textView);
    textView.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Bundle bundle = new Bundle();
            bundle.putInt("category",9);
            intent.putExtras(bundle); 
            startActivity(intent);
        }
    });

SecondActivity:

    package com.example.app;

    import android.content.Intent;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;


    public class SecondActivity extends AppCompatActivity {   

    ViewPager viewPager;
    TabsPagerAdapter pagerAdapter;
    private int mMedCategory = 6;

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

       // String stringCategory = getIntent().getStringExtra("category_string");
    int medCategory = getIntent().getIntExtra("category_int", -1);
    setMedCategory(medCategory);

    viewPager = findViewById(R.id.viewpager);
    pagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(pagerAdapter);

    }
    public int getMedCategory(){
        return mMedCategory; //This value goes to TabsPagerAdapter
    }

    public void setMedCategory(int i){
        mMedCategory = i;
    }
    }

4 Answers4

1

Use Intent#getIntExtra() to retrieve an integer value from the intent:

From MainActivity:

Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
myIntent.putExtra("category", 9);
MainActivity.this.startActivity(myIntent);

From SecondActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    medCategory = intent.getIntExtra("category", -1);
}

Note that this would assign a default value of -1 to medCategory in the event that the key cannot be found.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can try this:

MainActivity

TextView textView = findViewById(R.id.textView);
textView.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("category_int", 9);
        intent.putExtra("category_string", "9");
        startActivity(intent);
    }
});

SecondActivity

private int medCategoryInt;
private String medCategoryString;

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

    medCategoryInt = getIntent().getIntExtra("category_int", 0);
    medCategoryString = getIntent().getStringExtra("category_string");
}

You can have a look at How do I pass data between Activities in Android application?

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
  • This helped in narrowing down the issue, so I am getting correct values in in medCategoryInt and medCategoryString variable. However private int medCategory; becomes 0 on it's own, is there something wrong in the way I am declaring medCategory variable. – Ankit Chauhan Feb 10 '18 at 07:13
  • @Ankit Chauhan, you can have variable private also. see my updated answer.... still you getting issue? – Rumit Patel Feb 10 '18 at 07:26
  • That's the issue, whatever I put as global variable becomes 0. I also tried assigning value 6 to private int medCategoryInt and that value doesn't change. Seems like private int medCategoryInt is static, and it's not possible to change it's value. – Ankit Chauhan Feb 10 '18 at 07:34
  • it is not static. It should not happen. Update your question with whole SecondActivity.java – Rumit Patel Feb 10 '18 at 07:38
  • Updated question with whole SecondActivity.java – Ankit Chauhan Feb 10 '18 at 07:50
  • ohk... i see. a little mistake... remove 'int' from 'int medCategory = getIntent().getIntExtra("category_int", -1);' your issue will be soved. – Rumit Patel Feb 10 '18 at 08:01
  • just use medCategory = getIntent().getIntExtra("category_int", -1); – Rumit Patel Feb 10 '18 at 08:02
  • No there is a difference in name, one is "mMedCategory" and another one is "medCategory". – Ankit Chauhan Feb 10 '18 at 08:08
  • is there same key you are using at both activities? "category_int"? confirm that. – Rumit Patel Feb 10 '18 at 08:12
  • Yes, it's same key. I have started getting this warning now "Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?" :) – Ankit Chauhan Feb 10 '18 at 08:14
  • come to chat room : https://chat.stackoverflow.com/rooms/info/164876/android-help-discussion?tab=general – Rumit Patel Feb 10 '18 at 08:24
  • You must have 20 reputation on Stack Overflow to talk here. :/ – Ankit Chauhan Feb 10 '18 at 08:30
  • Let me start a project from scratch, probably it's a very silly mistake I am making. Appreciate your time and help. – Ankit Chauhan Feb 10 '18 at 08:45
0

Use this code. Remove final and try to initialize intent in onclick method not outside of it.

In your OnClick

Intent intent=new Intent(this,Main2Activity.class);
    Bundle bundle = new Bundle();
    bundle.putInt("category",9);
    intent.putExtras(bundle);
    startActivity(intent);

In your second activity

public class Main2Activity extends AppCompatActivity {

private int mMedCategory =0;

public int getmMedCategory() {
    return mMedCategory;
}

public void setmMedCategory(int mMedCategory) {
    this.mMedCategory = mMedCategory;
}

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

    Bundle bundle=getIntent().getExtras();
    if(bundle!=null){
        int demoCategory =bundle.getInt("category");
        setmMedCategory(demoCategory);
    }else {
        Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
    }
}

public void Show(View view) {
    Toast.makeText(this, String.valueOf(getmMedCategory()), Toast.LENGTH_SHORT).show();
}

}

EDIT

Created a demo project for you it is working. Show method is onclick method of a button.

Zankrut Parmar
  • 1,872
  • 1
  • 13
  • 28
0

I'm not sure but You may use this for second activity:

SecondActivity secondActivity = new SecondActivity();
    if (secondActivity.getIntent.getExtras() != null)
    {
        int medCat = secondActivity.getIntent.getExtras().getInt("category");
    }
Orhan
  • 169
  • 1
  • 7