1

I have 5 activities, say activity A,B,C,D and E.

Each activity has two buttons yes & no,buttons have the data which I want to pass to activity E only.

I need to do following things:

--> When user press yes/no button of A_activity, the user move to B_activity but data passed to activity E via intent. Similarly at activity B user press yes/no button, user will move to activity C but data passed to activity E and so on.

I have done lot of search but couldn't find solution is there any way to do this.

Rucha Bhatt Joshi
  • 822
  • 1
  • 15
  • 38
AliShah
  • 39
  • 1
  • 7
  • You have to launch/move to the desired activity via intent and pass the data via `SharedPreferences` Check this out: https://stackoverflow.com/questions/15466673/how-to-send-data-through-intent-in-android-without-opening-another-activity – Zakir Jun 05 '17 at 23:12
  • @zakir I am new to programming, first i will learn how to use SharedPreferences to pass data. I will learn it and if there is any problem i will let you know. but is there any other way to do so via intend. – AliShah Jun 05 '17 at 23:20
  • Looks like the moment you pass an intent to an Activity it is going to start - no was o avoid it - or am not aware of it.. – Zakir Jun 05 '17 at 23:22
  • @Zakir passing intent to activity works perfectly but my conundrum is that if user is at activity A, on pressing yes/no button, I want to pass data to last activity i.e E activity + user move to next activity i.e activity B. I have seen the examples where data + user move to next activity but my scenario is bit different to examples shared on internet. I just want user to move very next activity but data passes to last activity i.e E in my case. – AliShah Jun 05 '17 at 23:54
  • You can't pass data like that using intents that you are moving to one activity and passing data to another. Either you can use SharedPreferences or you can use Sqlite db. – sumit Jun 06 '17 at 03:15
  • 1
    post your code what you have tried – Aditya Vyas-Lakhan Jun 06 '17 at 04:12

2 Answers2

0

Use SharedPreferences class to pass the data:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key", "Value");
editor.commit();

Then in the Activity E:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String value = sharedPref.getString("key", defaultValue);

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
-1

You can indeed pass data back through an intent to an Activity's onActivityResult()

Activity E starting Activity A

final int RESULT_FOR_CLASS_DATA = 12; // pick a number to use
String returnedData;

Intent intent = new Intent(this, ActivityA.class);
startActivityForResult(intent, RESULT_FOR_CLASS_DATA);

Example in A (started from E)

Intent data = new Intent();
data.putExtra("ReturnData", dataToReturn); 
setResult(RESULT_OK, data);
finish(); // returning to Activity E

Example in onActivityResult() of E

if (requestCode == RESULT_FOR_CLASS_DATA) {
    returnedData = data.getStringExtra("ReturnData"));
}

There's a wide variety of ways to accomplish moving data around though so this is just one example.

To recommend an alternative, use Handler instead of this. Supposed that since the return Activity is paused, handler might require setting data in a separate class or global static variables, etc. and retrieved on Activity restart or something. So its possible to do with little to no code duplication in any class. Just retrieve and use. Whereas Intent requires code duplication in every class. It would also easily allow going from any Activity to any Activity like A->B->E->C instead of requiring E->A->E->B. Just something to think about.

CmosBattery
  • 934
  • 7
  • 13