-4

I want to pass data from one activity to second but without start this second activity. Because with Intent we have to start the activity with an intent, but I want to not start this activity. How can I do that?

Radek
  • 1
  • 1
  • Follow this [Link](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) you can also pass objects that is explained in this [Link](https://stackoverflow.com/questions/7145606/how-android-sharedpreferences-save-store-object) – Mahsa Oct 23 '17 at 19:14
  • Why do you want to send the data to the activity if you are not intending to launch it? What is the purpose of sending that information to the other activity. If it is for storing data then you can use Sqlite or SharedPreference for settings. – Soumya Oct 23 '17 at 19:49
  • To practice, I want to create an app to learn words from foreign languages, so first activity will be a "menu", in menu it'll be 2 buttons to start 2 other activities. First activity will be an activity with words, and then I check that I know this words or no. And then in menu will be button that launch activity that shows the words that I checked I don't know ;p I think you'll understand – Radek Oct 23 '17 at 21:17

3 Answers3

1
SharedPreferences preferences = 
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

String myString = preferences.getString("myString", "N/A");
int myInt = preferences.getInt("myInt", -1);

U may use shared preferences

Kutlu Ozel
  • 182
  • 1
  • 7
0

It doesn't sound logical of passing data to the second activity while it's not launched. But it can be done in many ways, like:

  1. via setting variables of the second activity (a bad way);
    SecondActivity.someValue = value; // for static variables
    or, (new SecondActivity()).someValue = value; // for non-static variables

  2. putting data into the shared-preference and then using that from wherever you want.
    In your first-activity:
    SharedPreferences.Editor prefEditor = PreferenceManager .getDefaultSharedPreferences(context).edit(); prefEditor.putString("key", "value"); prefsEditor.commit();

    In your second-activity:
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); String mValue= pref.getString("key", "default-value");

Touhid
  • 731
  • 1
  • 10
  • 25
  • Okay, maybe it doesn't sound logical, because I don't said what I want to program. To practice, I created an app to learn words from foreign languages, so first activity will be a "menu", in menu it'll be 2 buttons to start 2 activities. First will be an activity with words, and then I check that I know this words or no. And then in menu will be button that launch activity that shows the words that I checked I don't know ;p I think you'll understand it – Radek Oct 23 '17 at 21:15
0

You probably need to re-think your design. An activity is a UI component. If there's no UI, there's no point in starting the activity.

If you just need to "do some work", look at using an IntentService. You can start it with an intent similar to how you start an activity. It has no UI.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134