通过意图传递数据或在需要时查询数据库是否更好?

时间:2022-07-28 16:29:25

I am just wondering what would be the better way to handle data in several activities in android.

我只是想知道在android中的几个活动中处理数据的更好方法是什么。

Say I had two activities, A and B, that hold some views. First I load some data from a SQL database and inflate the views in A. Now, I want to start activity B, which uses the same set of data as A did.

假设我有两个活动,A和B,持有一些观点。首先,我从SQL数据库加载一些数据并在A中膨胀视图。现在,我想启动活动B,它使用与A相同的数据集。

Is it better to pass the data via Intent (putExtra()) and then inflate the views or is it better to query the database again and then inflate.

通过Intent(putExtra())传递数据然后膨胀视图或者再次查询数据库然后膨胀​​更好。

I am not sure about that, because both approaches seem to have their disadvantages:

我不确定,因为这两种方法似乎都有它们的缺点:

  • Querying the database takes more time /more resources
  • 查询数据库需要更多时间/更多资源
  • Putting extra data to the intent makes it more complex, because of putting and getting the data (especially when working with more activities)
  • 将额外数据放入意图会使其更复杂,因为放置和获取数据(特别是在处理更多活动时)

Can someone give me some advice on what is the best practice?

有人可以就什么是最佳做法给我一些建议吗?

2 个解决方案

#1


2  

As compare to DB Query use Intent.

与DB Query相比,使用Intent。

And another way is, use one common class which will holds your data temporary.

另一种方法是,使用一个公共类,它将暂时保存您的数据。

#2


0  

There is various way to pass and get data. It is more useful to use Intent than DB query.

有多种方式来传递和获取数据。使用Intent比使用DB查询更有用。

But there is another useful way is shared prefernce. Through which you can create,edit, delete data as well as can fetch data from any of the activity.

但另一种有用的方式是共享偏好。您可以通过它创建,编辑,删除数据以及从任何活动中获取数据。

To create or edit shared preference:

要创建或编辑共享首选项:

String share_pref_file = "your_file_name";      
SharedPreferences prefs1 = getSharedPreferences(
        share_pref_time_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefs1.edit();
editor1.putString("your_data", data); //data is your variable
editor1.commit();

To fetch data:

要获取数据:

String share_pref_file = "your_file_name";
SharedPreferences prefs = getSharedPreferences(share_pref_file,
    Context.MODE_PRIVATE);
String strJson = prefs.getString("your_data", "");

To delete:

删除:

String share_pref_file = "your_file_name";
SharedPreferences prefs1 = getSharedPreferences(
            share_pref_file, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs1.edit();
    editor.remove(share_pref_file);
    editor.clear();
    editor.commit();

#1


2  

As compare to DB Query use Intent.

与DB Query相比,使用Intent。

And another way is, use one common class which will holds your data temporary.

另一种方法是,使用一个公共类,它将暂时保存您的数据。

#2


0  

There is various way to pass and get data. It is more useful to use Intent than DB query.

有多种方式来传递和获取数据。使用Intent比使用DB查询更有用。

But there is another useful way is shared prefernce. Through which you can create,edit, delete data as well as can fetch data from any of the activity.

但另一种有用的方式是共享偏好。您可以通过它创建,编辑,删除数据以及从任何活动中获取数据。

To create or edit shared preference:

要创建或编辑共享首选项:

String share_pref_file = "your_file_name";      
SharedPreferences prefs1 = getSharedPreferences(
        share_pref_time_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefs1.edit();
editor1.putString("your_data", data); //data is your variable
editor1.commit();

To fetch data:

要获取数据:

String share_pref_file = "your_file_name";
SharedPreferences prefs = getSharedPreferences(share_pref_file,
    Context.MODE_PRIVATE);
String strJson = prefs.getString("your_data", "");

To delete:

删除:

String share_pref_file = "your_file_name";
SharedPreferences prefs1 = getSharedPreferences(
            share_pref_file, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs1.edit();
    editor.remove(share_pref_file);
    editor.clear();
    editor.commit();