-1

There are 3 activities named activity-one, activity-two and activity-three. Clicking next on activity-one takes you to the activity-two and then next on activity-two takes you to activity-three. So, I want to send object of my model class from activity-one to activity-three directly. How can this will be achieved?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • there is no *good* way to do that. A good way would be to send it through intents with activity-2 in the middle. The bad way can be using a static field in the activity 3 for example – Vladyslav Matviienko May 23 '19 at 10:38
  • If you are going trough `activity one->two->three` then why are you not sending data to `activity two->three`? – Nick Bapu May 23 '19 at 10:39
  • Either you can propagate your model from A -B - C. Its the most conventional and android way. Another way is you can make a method in your application class to hold your model .Set it from Activity A and fetch it again from Activity C – p.mathew13 May 23 '19 at 10:39
  • @p.mathew13 can give some example for making method to hold model? – Akash Jaiswal May 23 '19 at 10:46
  • use static variable – raj kavadia May 23 '19 at 10:54
  • try this https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android – atarasenko May 23 '19 at 11:00

2 Answers2

0

Try this:

My ApplicationClass extends Application{
 private Model model;
 public static void setModel(Model model){
 this.model=model
 }

 public static Model getModel{
 return model
}
}

You can use generics to generify this function to handle any kind of model classes

p.mathew13
  • 920
  • 8
  • 16
0

Another way is using

SharedPreferences

like this

In Activity one

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

In Third Activity

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
 String name = prefs.getString("name", "No name defined");//"No name defined" is the 
 default value.
 int idName = prefs.getInt("idName", 0); //0 is the default value.
}
Muhaiminur Rahman
  • 3,066
  • 20
  • 27