-2

I'm learning Android and trying to pass user input from my first activity to several activities but not the second activity. actually I'm going to get more data in my second activity and later use that also in my later activities but not in order. How can I do that?

alast
  • 15
  • 2
  • 8
  • There's no difference if you pass data to one Activity or another. You can only call startActivity once, though. – OneCricketeer Nov 26 '16 at 18:15
  • If you plan on sharing a lot of data, Intents aren't the best idea. SharedPreferences or Sqlite may be better depending on your needs – OneCricketeer Nov 26 '16 at 18:16
  • You could also use a global class and global variables to pass them along activities – Andrew Nguyen Nov 26 '16 at 18:17
  • @AndrewNguyen Backed by SharedPreferences. Please. – Eugen Pechanec Nov 26 '16 at 18:18
  • http://stackoverflow.com/questions/9529302/what-is-more-efficient-static-data-passing-shared-preferences-database – Andrew Nguyen Nov 26 '16 at 18:26
  • @cricket_007 so do you mean if i do the intent on first activity and then i start the secondActivity i can later receive the intent in 3rd or fourth activity? – alast Nov 26 '16 at 18:27
  • You can pass intent extras along to more than one Activity, yes. Call getIntent in the second activity, add data to it, then startActivity with the newly created intent for the third activity. But I wouldn't recommend it because you can lose data along the way if not careful. – OneCricketeer Nov 26 '16 at 18:34

1 Answers1

0
you make the variable as static or you cab send data as extras.
Set data in intent:
Intent  intent = new Intent(FirstActivity.this,SecondActiviy.class);
intent.putExtra("key",value);
startActivity(intent);

Fetch data from intent: 

String data = getIntent().getStringExtra("key");
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
  • thanks Rohit, but what if i need to receive my data from activityOne in activityFour? while i have to start the activityTwo after activityOne. that makes me confused. hope that makes sense? – alast Nov 26 '16 at 18:44
  • In that case you need to make the variable as static – Rohit Heera Nov 26 '16 at 18:52