How to save List<UserDefinedClass> of between Activity switching ? I get data in onClickView function. Does UserDefinedClass need to implements some interface ?
I believe the best way would be to keep the list in the Application context.
In any of your activitys call yourApp using:
((yourApp)getApplication()).//any public function
public yourApp extends Application{
private List<UserDefinedClass> yourList;
//create functions to manipulate the list example:
public void add(UserDefinedClass a){
//add this to list
}
//finally function to access list
public List<UserDefinedClass> getList(){
return yourList;
}
}
//example:
List<UserDefinedClass> aList = ((yourApp)getApplication()).getList();
It is best to only pass an identifier to the next Activity instead of passing your entire list.
UserDefinedClass should not implement some interface, rather UserDefinedClass should be the class type of the List. In other words, every item in your list should be of UserDefinedClass. That can be a String or even a custom class.
Yes you can pass it if it implements Parcelable. However if its a large amount of data a database or file may work out better for you.
Related
I have some major variables in my main activity that I retrieve their values from internet once the user log in. after that, there are multiple activities that can alter these values in the database but this doesn't reflect in the current loaded values until the user log out/in. (the process where I retrieve data from internet)
what is the best way to update these values in main activity from activity 2 directly without logging out/in?
is there a way to set these variables without making intent and putting the new values in extra bundle? ( I need to change the values without returning back to the activity)
You can perhaps try putting the variables in a custom Application class as static members and then access them from anywhere through Application.xyzStaticMember(). Here's an example:
public class MyTestApp extends Application {
private static List<Int> testList;
public static void setList(List<Int> l) {
testList = l;
}
public static List<Int> getList() {
return testList;
}
}
Then access the members: MyTestApp.setList(null); or MyTestApp.getList();. Oh, and don't forget to use the class in the AndroidManifest.xml file!
<application android:name="com.example.MyTestApp"
/* ...more stuffs */ />
I am trying to do something like that , Firstly , there is a MainActivity which stores courses in ArrayList and these courses are showed by listview.After that , I pass to another activity which is called as ShowDetailActivity with startActivity() to show course details when I click list view element.Then, I pass to another activity which is called EditCourseActivity to edit course which is shown by ShowDetailActivity. The problem is that When I want to edit course , I have to access MainActivity 's Arraylist (private) but I cannot pass with startActivity() because MainActivity did restart (reinstalled) that's why there is no courses on arraylist. The question is How can access from EditCourseActivity to MainActivity 's arraylist to show updated courses ?
Store the course information in a singleton class
public class CourseHolder{
public static CourseHolder instance=null;
public static CourseHolder getInstance(){
if(instance==null){
instance=new CourseHolder();
}
return instance;
}
private Course courses[];
private CourseHolder(){
courses=new Course[10];
}
public void setCourse(int index, Course course){
courses[index]=course;
}
public Course[] getCourses(){
return courses;
}
}
In the EditCourseActivity, after editing, store the updated course information in the copy of CourseHolder
In MainActivity.onResume(), call listView.setAdapter(null), then refresh it with the data inside the CourseHolder
As a generic answer: detach the data from its representation. You can achieve this in many ways:
Singleton wrapper for your array list
To be android specific: wrap your array in a ContentProvider
Use Android SDK SQL database support
I am new at developing Android .I have a question. How to pass object from one activity to another activity without using Intent.Can I do it by Interface ,if so how Could you please how can I hanle that
I think you have 2 options
In memory, save it to somewhere that all activities can reach, or make it static. This is not good idea though
Save it to disk, and use it, such as shared preferences
You can store it in SharedPreferences and then in another Activity
restore it.
You can store it in SQLite and then in another Activity restore it.
You can use static links
You can use service
save data in a singleton class model and get the same object from another activity
Create a class like this
public class SingletonModel {
private static SingletonModel instance;
public String textData = ""
public synchronized static SingletonModel getSingletonModel() {
if (instance == null) {
instance = new SingletonModel();
}
return instance;
}
private void SingletonModel(){}
}
From first activity do like this
SingletonModel.getSingletonModel().textData ="Your data goes here";
From second activity do like this
textView.setText(SingletonModel.getSingletonModel().textData);
If the data should persist, use a file. If not, use a singleton.
I'm developing an android application that deals with bitmaps. Right now I'm using an LRUCache to store these bitmaps, but there are going to be multiple fragments that require access to this cache. What's the best way to handle to handle a global application cache? Should I use a singleton? Should I create a new cache for each activity/fragment that requires access to it?
To avoid Singletons in Android Development, like in your question I would suggest anybody using Fragments to store real global Objects in the MainActivity. It should look like this:
MainActivity:
public class MainActivity extends FragmentActivity{
//eg a ArrayList
private ArrayList<MyObjects> mObjectContainer = new ArrayList<MyObject>();
//Do something with the list and fill it
public ArrayList<MyObjects> getObjectContainer(){
return mObjectContainer;
]
public void addObject(MyObject object){
if(!mObjectContainer.contains(object)){
mObjectContainer.add(object);
}
}
}
Fragment:
public class MyFirstFragment extends Fragment{
onCreateView(...){
//make typical Fragment init like create View
ArrayList<MyObjects> list = ((MainActivity)getActivity()).getObjectContainer();
//add a new Object
((MainActivity)getActivity).addObject(new MyObject("any data"));
}
}
I have a problem about setting a TextView from a class which is not a child class of Activity. This class is basically used for handling registration and REST request with 3rd party server.
After getting textfield info from 3rd Party server, it is too late to set TextView in the Main Activity.
I can't use SharedPreferences to set this info, because MainActivity has already started.
I can't pass this info with Bundle since my java class is not an activity class.
How can I pass this info and set the TextView in the MainActivity? Is there any way to do this?
The proper way of doing this this is to create a listener.
Create an interface :
public interface OperationCompletedListener{
void onOperationCompleted(String resultValue);
}
Then in your class which calls Rest services, create a variable for this listener and a method to set it.
private OperationCompletedListener mListener;
public void setOperationCompletedListener(OperationCompletedListener listener){
mListener=listener;
}
Then when the your rest service completed call like below :
if(mListener!=null){
mListener.onOperationCompleted("your value to be passed");
}
Then in your activity class which contains the TextView, create an object of OperationCompletedListener and set it to the other class using the set method that we created earlier. Then in the onOperationCompleted method, set the text view with your value and you are done.
private OperationCompletedListener mOperationCompletedListener=new OperationCompletedListener() {
#Override
public void onOperationCompleted(String resultValue) {
yourTextView.setText(resultValue);
}
};
restServiceClassObject.setOperationCompletedListener(mOperationCompletedListener);
You can create an static method which update textview in your activity class . Then call this method from your other class whenever you want.
Try to pass the Activity to the non-Activity class when you instantiate it. For example:
public class NonActivityClass {
private Activity parentActivity;
public NonActivity(Activity parentActivity) {
this.parentActivity = parentActivity;
}
}
Or you can just pass the Activity to a static method in your NonActivityClass if you don't want to instantiate it (it's abstract). Then, you can inflate the TextView or do a findViewById from the parent and set the text.
From my experience, you should never use a static non-final variable to maintain a reference across activities. When you restart the app or the phone, or when Android kills your app's process, the reference and state of the variable becomes lost and may cause your app to crash.