-3

What the title says, I've tried below but it didn't work. Also where should I put the getExtras() in MainActivity.class? I tried it both outside and inside OnCreate() but my app keeps crashing upon startup. I can't find an error with it crashing either.

SettingsActivity.class

public void onClick(View v) {
                Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
                intent.putExtra("isMeasurement", 2);
                startActivity(intent);
            }

MainActivity.class

public int isMeasurement = getIntent().getExtras().getInt("isMeasurement");
Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Flies
  • 29
  • 3
  • It would be available in SettingsActivity – Antoniossss Apr 05 '19 at 13:13
  • That doesn't make any sense? There should be some code in MainActivity, and some code in SettingsActivity which is what I have – Flies Apr 05 '19 at 13:17
  • You shouldn't call `getIntent()` before `onCreate()` which seems to happen here when you initialize the `public int isMeasurement` field. See: https://stackoverflow.com/a/13984384/6761823 – Andrzej Zabost Apr 05 '19 at 13:18
  • @Flies is it your data flow like MainActivity->SettingsActivity->MainActivity ???? – Basi Apr 05 '19 at 13:28
  • Yeah, but I have it all fixed now, thanks guys :) Also, is there something wrong with my questions (like being too dumb or something)? They get downvoted all the time. Not anything to worry about, just curious – Flies Apr 05 '19 at 13:32

3 Answers3

2

This is how you should pass data

Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
            intent.putExtra("isMeasurement", 2);
            startActivity(intent);

Inside your onCreate

This is how you can retrieve data in MainActivity

int isMeasurement = getIntent().getIntExtra("isMeasurement",0); // Here 0 is a default value.It could be anything acc to your requirement
p.mathew13
  • 920
  • 8
  • 16
  • It doesn't change the fact that `getIntent()` should not be called before `onCreate()`. See: https://stackoverflow.com/a/13984384/6761823 – Andrzej Zabost Apr 05 '19 at 13:21
  • I never mentioned that getIntent() should be called before onCreate. Sorry if my answer was not clear. Have edited it to remove any confusion. – p.mathew13 Apr 05 '19 at 13:25
  • Yes, but `public int isMeasurement` clearly indicates it's a field that gets evaluated before `onCreate()`. You can't use the `public` modifier like this inside a method. – Andrzej Zabost Apr 05 '19 at 13:27
  • 1
    you are correct. My mistake for not noticing it before. Thanks for pointing it out @AndrzejZabost – p.mathew13 Apr 05 '19 at 13:29
0

There is problem in your getting data in other activity. The correct way to do is mentioned below :

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    int isMeasurement = getIntent().getIntExtra("isMeasurement",0);
}
Hitesh Sarsava
  • 666
  • 4
  • 15
0

SettingsActivity:

Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
            intent.putExtra("isMeasurement", 2);
            startActivity(intent);

and

MainActivity:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
    int isMeasurement = getIntent().getIntExtra("isMeasurement",0);
}
Basi
  • 3,009
  • 23
  • 28