-7

Intent leads to create new activity so I do not want to use intent for data transfer. There should be seamless data exchange between activities.

Pushpendra
  • 2,791
  • 4
  • 26
  • 49
  • 1
    then use persistent data ... also intents are seamless – Selvin May 22 '17 at 10:48
  • 2
    `shared preferences` ? – Priyesh Kumar May 22 '17 at 10:48
  • 2
    You can make data or variables as public static. – R.R.M May 22 '17 at 10:49
  • 4
    *Then make and use interface.* @NitinPatel no, this is a bad idea ... you should think llike "there is no 2 Activities at the same time" ... public static data are also bad idea as you may return to the second activity after process ends(and restarts) and static data will be obviously null – Selvin May 22 '17 at 10:50
  • @NitinPatel interface applies between activity and fragment not activity to activity. – Pushpendra May 22 '17 at 10:51
  • 1
    Best way is just save your data at application level. – M D May 22 '17 at 10:59
  • EventBus - https://stackoverflow.com/questions/43799099/how-do-i-call-a-childs-method-from-the-parent-class/43799658#43799658 – Jie Xu Jul 06 '17 at 09:08

3 Answers3

0

You can use sharedpreferences helper ( u can access anywhere in your app. ) Second, you can make singleton Model(that u can access from anywhere). But here once you destroyed app it will also destroy but in preferences helper, u can get data back.

Mohit Kacha
  • 484
  • 2
  • 12
0

Try this for saving shared Preferences:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    sharedPreferences.edit().putString("key","value").apply();

For getting shared preferences value:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    sharedPreferences.getString("key","");

I hope this will help.

Ashish Gupta
  • 737
  • 11
  • 18
0

I got a very easy solution.
A BroadcastReceiver can solve my problem.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103