ActivityObjects is a Android library to simply pass Java-Objects from one Activity to another
All passed Objects are integrated in Android Lifecyle automatically (explained below)
We will create Activitys by AbstractActivityObjects instead by AppCompatAcvtivity
public class MyActivity extends AbstractActivityObjects { // AbstractActivityObjects extends AppCompatActivity
}First create a Object[] or List where all your Objects to pass are included
List<Object> objectList = new ArrayList<>();
objectList.add(new MyOwnObject("cool objects", 3));
objectList.add(new MyOwnObject("cool objects 2", 7));Now just call
public static Intent passObjectsToActivity(Context from, Class to, List<Object> objects);
public static Intent passObjectsToActivity(Context from, Class to, Object[] objects);from ActivityObjects
Intent intent = ActivityObjects.passObjectsToActivity(this, SecondActivity.class, objectList);
//here you can add more to the Intent (e.g. flags)
startActivity(intent);E.g. when the android device will rotate the current Activity will finished and recreated with savedInstances
You have a Object created in a Activity. Let us call him surviver, but android device rotated :(
Surviver was killed...
Ok. Let's start new :D
In onCreate method are savedInstances
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null) { //Activity first launch
surviver = new MyOwnObject("I will survive!", 100);
} else { //Activity recreation
surviver = (MyOwnObject) getSavedActivityObject(savedInstanceState, "surviver key");
}
...
}We will check if savedInstances are null (Activity first start)
If true create the surviver, if false getSavedActivityObject
But stop!!! Where comes getSavedActivityObject from?
public class MyActivity extends AbstractActivityObjects { // AbstractActivityObjects extends AppCompatActivity
}You must only put the surviver before in onSavedInstanceState
@Override
public void onSaveInstanceState(Bundle outState) {
putSavedActivityObject(outState, "surviver key", surviver);
super.onSaveInstanceState(outState);
}No license :D !!! Feel free to share, copy, fork, ...