简体   繁体   English

如何在多个活动之间共享数据?

[英]How to share data between multiple activities?

This will be my first project in android studio. 这将是我在android studio中的第一个项目。 I have a some java and C/C++ experience. 我有一些Java和C / C ++经验。 I still am a total beginner though when it comes to program development. 尽管在程序开发方面,我仍然是一个初学者。 I have never developed an android app. 我从未开发过android应用。

This app will be used to track a workout program. 该应用程序将用于跟踪锻炼计划。 User will be required to enter his 1 rep maxes for each of the following; 用户需要为以下各项输入他的1个代表最大值; bench, squat, deadlift. 板凳,深蹲,硬拉。 I want the app to use these inputs to base the workout program on them. 我希望该应用程序使用这些输入来使锻炼程序基于它们。 This will involve multiple activities sharing and manipulating this data. 这将涉及多个活动,这些活动共享和操纵该数据。

What is the best way to take the user input, save it (meaning store it somewhere and left unchanged even when the app terminates), and have multiple activities access it to display? 接收用户输入,将其保存(意味着将其存储在某个位置,即使应用终止时也保持不变)以及具有多个活动可显示的最佳方法是什么? The app will only ask the user for input once. 该应用程序只会要求用户输入一次。 If the user wants to change input, it can be done in settings. 如果用户想更改输入,则可以在设置中完成。

When you call Intent to jump from one activity to other activity then you can put some data or values which you need for second activity or any further use. 当您调用Intent从一个活动跳到另一个活动时,您可以放置​​一些数据或值,这些数据或值是第二个活动或任何其他用途所需的。 You have to do same for all activity call and lastly you can use it where you required data. 您必须对所有活动调用执行相同的操作,最后可以在需要数据的地方使用它。

intentObject.putExtra("KEY", value);

Bundle bundle = getIntent().getExtras();

int value = bundle.getInt("KEY");
or
String value = bundle.getString("KEY");
or
DataObject dataObject = bundle.getString("KEY");

You can put data object and get data object also using Gson. 您也可以使用Gson放置数据对象并获取数据对象。

Try this by using INTENTS 通过使用INTENTS试试

On First Activity 第一次活动时

Intent intent = new Intent(Activity_FROM.this, Activity_TO.class);

intent.putExtra("KEY1", value);

intent.putExtra("KEY2", "a value");

startActivity(intent);

On second activity

Bundle bundle = getIntent().getExtras();

int value = bundle.getInt("KEY1");

String value2 = bundle.getString("KEY2");

To have it more clearer, you better to understand basic concepts of android framework and terminologies. 为了更清楚,您最好了解android框架和术语的基本概念。 When its done, you can easily understand the process flow. 完成后,您可以轻松了解流程。

Visit www.vogella.com 请访问www.vogella.com

Yes, Intents are the way to pass information between activities, but as you will soon discover, they are not meant to hold whole objects of data and to keep some kind of persistence, trying to implement your data passing will become your nightmare. 是的,Intent是在活动之间传递信息的方式,但是正如您将很快发现的那样,Intent不是要保留整个数据对象并保持某种持久性,试图实现数据传递将成为您的噩梦。

Take a look for some persistence frameworks( let it be some SQLite wrappers or I personally like realm.io as they provide really cool features like live auto-updating objects, it's built upon their c++ engine and it's very easy to use )... Once you store your data in some kind of data layer, you only pass identifications for your objects through intents and query for them in the receiving activity... 看看一些持久性框架(让它成为一些SQLite包装器,或者我个人喜欢realm.io,因为它们提供了诸如实时自动更新对象之类的非常酷的功能,它是基于其c ++引擎构建的,并且非常易于使用)...一旦将数据存储在某种数据层中,就只能通过意图传递对象的标识,并在接收活动中查询它们。

That way, you keep your model persistent across the entire application, don't have the trouble to propagate changes of the object back to the source activity and lose the hell related with Parcelables ( with is a way to serialize your object to store it in intent ) 这样,您就可以在整个应用程序中保持模型的持久性,而不必麻烦地将对象的更改传播回源活动,而不必担心与Parcelables相关的麻烦(通过这种方法可以序列化对象以将其存储在意向)

Happy coding! 编码愉快!

Intents are one way to pass Data from one activity to another.However you need to pass it every time you change from one to other... Some other way more simple and effective is to use a singleton : a class where you can write into and access in any activity by creating an instance for it. 意图是将数据从一个活动传递到另一个活动的一种方法。但是,每次从一个活动更改为另一个活动时,都需要传递数据。另一种更简单有效的方法是使用单例:您可以在其中写入的类并通过为其创建实例来访问任何活动。 just take a look at this https://gist.github.com/Akayh/5566992 只是看看这个https://gist.github.com/Akayh/5566992

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM