0

Sorry if this is obvious but I am very new to andriod (just playing around trying to learn something new).

I think this is a fairly common thing to do, but I am having trouble figuring out the best way to do it.

Right now I have 3 activities. A main activity that will start a second activity that shows a list of objects, and finally a third activity, started from the list activity, where I want to make changes to the selected object. Both the list and details activities host fragments.

Is there some safe place I store the selected object and retrieve it from the details activity/fragment? It seems like the main activity is the only common link, but I am not sure I can count on that activity not having been destroyed.

Do I need to pass it via an Intent? This seems it would work but also seems overly complicated. (I am thinking I have to serialize the object and return it as a result of the activity)

Should I just combine the final 2 list and details activities into one activity, and swap out the fragments? I think if I do that I can safely store the selected object in the combined activity, is that correct?

4

1 回答 1

0

常见且正确的做法:

您必须创建一个对象(您将传递)并为该对象实现 Parcelable 接口(例如),然后按照您的意图传递它intent.putExtra("identifier", youParcelable)

例子:

在启动新 Activity 时添加一个对象:

Intent intent = new Intent(getApplicationContext(),
yourActivityClass.class);
intent.putExtra("identifier", youParcelable);
startActivity(intent);

在新打开的 Activity 中从意图中检索对象:

Object object = getIntent().getExtras().getParcelable("identifier");

资源

于 2013-08-03T16:07:13.467 回答