For this project, I'm using Android Studio. I've tried a bunch of things.
Goal: I would like to create an application which asks the user to input data. The data needs to be saved and later be called on another screen as a log history which the user can see.
What I've done: I have three classes: "InputScreen" "Logs" "LogHistoryScreen"
I've tried to create Logs object at InputScreen which works perfectly fine such as: Logs log1 = new Logs(); But I have no idea how to call the object log1 made in InputScreen from the LogHistoryScreen. Anyone who have any suggestions?
Thanks in advance.
Create a list of logs in InputScreen class:
List<Log> logs = new ArrayList();
logs.add(log1);
// same for next logs.
then create a method in InputScreen which will return the list of logs. Something like this:
public List<Log> getAllLogs() {
return logs;
}
Call this method from LogHistoryScreen like:
InputScreen inputScreen = new InputScreen();
List<Log> logs = inputScreen.getAllLogs();
There are various options you could use here. I will mention a few here.
1) Passing through Intent.
Pass the Object as a parameter while you open the LogHistoryScreen from the InputScreen.
Please follow the below code to do so.
Intent intent = new Intent(this, LogHistoryScreen.class);
intent.putExtra("Key", yourObject);
startActivity(intent);
Receive the Object in the onCreate() of the LogHistoryScreen activity
Log log = (Log)getIntent().getSerializableExtra("Key");
The Log.class must implement Serializable.
public class Log implements Serializable
{
}
2) Store the Object in the Database and retrieve it from the other activity. This is particularly helpful if you need the data to persist across app sessions.
I think the first option will be more helpful for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.