-3

I am making my own project but I am stuck.

Getting the information from the user in the first activity is working just fine. But when I try to get those numbers, ints, in the third activity via a second activity, it is showing me the default value.

I am trying it using an Intent, but it is not working.

Intent intent = new Intent(this, active.class);
Intent intent2 = new Intent(this, BMR_Active.class);

intent.putExtra(EXTRA_TEXT_height,height );
intent.putExtra(EXTRA_TEXT_weight,weight);
intent.putExtra(EXTRA_TEXT_age,age);

startActivity(intent);

It is giving me a default value i.e. 0(I have given) in BMR activity then i am going via active activity.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
  • Are you passing values from your first activity to a second activity and then to a third activity? If so, then you can refer to [**this tutorial on how to use Intents with multiple activities.**](https://www.youtube.com/watch?v=fnS962yF44Q) It also covers how to pass data from the first page to the second page, then the second page to the third page. Or maybe you can check out this other StackOverflow question: https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click/4186097 – Ishaan Javali Jul 12 '19 at 22:55
  • no i am not passing the data from 1 to 2 activity and then 3,i want to do from 1 to 3 directly because it will minimize the code ,could please you help me with that? – shubham nayal Jul 13 '19 at 17:00
  • Sure. I still recommend that you check out [this tutorial](https://www.youtube.com/watch?v=fnS962yF44Q) because it covers it in detail with full explanations. But, all you have to do is Intent intent = new Intent(Page1.this, Page3.class); intent.putExtra("Number", ); – Ishaan Javali Jul 13 '19 at 17:07
  • Can you add the code of Activity 3, where you try to get the data from the intent? Perhaps there's something wrong there. – Ridcully Jul 13 '19 at 17:15
  • Intent intent = getIntent(); double height = intent.getDoubleExtra(MainActivity.EXTRA_TEXT_height,0); double weight = intent.getDoubleExtra(MainActivity.EXTRA_TEXT_weight,0); int age = intent.getIntExtra(MainActivity.EXTRA_TEXT_age,0); double bmrActive=66.47+(13.7*weight)+(5.003*height)-(6.755*age); TextView textView1 = (TextView) findViewById(R.id.activeBmr); textView1.setText(""+bmrActive); } – shubham nayal Jul 16 '19 at 18:45

2 Answers2

0

Your putExtra method via intent would work fine, but are you also running putExtra BEFORE starting the third activiy?

The data is first passed from first activity to second activity, then from second activity is passed to third activity using the same method.

Additional Note: In cases like this, I would usually use fragments instead, I would store the data in the activity and fragments will obtain the datas via getActivity(). This will eliminate the needs of multiple passing of data around.

Lee Boon Kong
  • 1,007
  • 1
  • 8
  • 17
0

Based on what you said in your comment about passing data straight from Activity 1 to Activity 3, then what you can do is the following:

Page1.class:

Intent toPage3 = new Intent(Page1.this, Page3.class);
int myNum = 5;
toPage3.putExtra("Number", myNum);
startActivity(toPage3);

Page3.class:

Intent receiveIntent = getIntent();
int numFromPage1 = receiveIntent.getIntExtra("Number");

I believe that this should answer your question. numFromPage1 will be an int containing the value from Page1.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23