Manos Rasidakis
Manos Rasidakis

Reputation: 3

Send data amongst three activities

I have 3 activities, that connect like this: A -> B -> C.

On A you input a text that will be used in C, then B opens and you input another text to be used in C. But when C opens, produces null errors.

I came up with a solution, to start A then go directly to C and onCreate of C, start B as if it was going from A to B.

Is there a better solution? Is my solution decent or will it cause more problems than it fixes? Thanks for any help.

Upvotes: 2

Views: 85

Answers (4)

Sergey Emeliyanov
Sergey Emeliyanov

Reputation: 6961

There are several options - Intent extras, SharedPreferences, SQLite database, internal/external storage, Application class, static data classes etc.

For situation you've explained, I recommend using SharedPreferences to store data, it's relatively easy approach and you can access stored data at any time from any Activity or class, here is a simple usage guide:

Input data into SharedPreferences:

SharedPreferences.Editor editor = getSharedPreferences("YourPrefsFile", MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.apply();

Get data from SharedPreferences:

SharedPreferences prefs = getSharedPreferences("YourPrefsFile", MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "");
  int idName = prefs.getInt("idName", 0);
}

Upvotes: 2

Jyoti JK
Jyoti JK

Reputation: 2171

I think, You can pass the input to next activity through intent.

Since it is a text input,

intent.putExtra("A_Activity_Input","YourinputString"); //in activity A

Then Activity B starts, Get the string using

String Input_A_Activity=getIntent().getStringExtra("A_Activity_Input");

After When you are starting C activity from B, send both data using intent

intent.putExtra("A_Activity_Input",Input_A_Activity);
intent.putExtra("B_Activity_Input","YourinputString");

In Activity C, Get the string from the intent.

Since you just need a data to process, Storing it in SQL or SharedPref or something else wastes memory

Upvotes: 0

vivek verma
vivek verma

Reputation: 1766

This is not a very good idea as their may be delays in starting the activity as you would be doing some other operations as well in the onCreate method of activity B and C. Will cause performance issues in future due to delay in showing the activity.

Things you can do...

  • If the data is very small lets say a key value pair then why not use shared preferences.
  • Or use a database to store the data as and when it gets created, after which when the activity is created query it and show it.

Upvotes: 0

Basil Battikhi
Basil Battikhi

Reputation: 2668

You have 4 solutions in this case the

  1. first one is which you mentioned
  2. the second one is using an intent with put extra in every activity that you will go from
  3. the third solution is using a static variables so that the variables will not be affected if activity reopened (not recommended)
  4. the fourth one you can use shared preference to save the data (which will be the best solution in my opinion)

Upvotes: 1

Related Questions