简体   繁体   English

在活动之间传递片段

[英]Pass fragments between activities

I want to make an application that can support portrait and landscape.我想做一个可以支持纵向和横向的应用程序。 The layout has two panes, on the left is the options and the right shows the result.布局有两个窗格,左侧是选项,右侧显示结果。 When an option is selected the right pane shows it.选择一个选项后,右窗格会显示它。 But for portrait there is not enough room, so a separate activity is needed.但是对于肖像没有足够的空间,因此需要单独的活动。 Each option produces a different type of fragment, so I don't want to make an activity for each option when all that changes between activities is what fragment is being added there.每个选项都会产生不同类型的片段,所以当活动之间的所有变化都是在那里添加的片段时,我不想为每个选项创建一个活动。 I want to pass a fragment from the main activity to the new one, how would I do this?我想将主要活动中的片段传递给新活动,我该怎么做?

EDIT: Moved what asker actually wants to top.编辑:移动了询问者真正想要的内容。

If you want to pass data to an Activity when creating it, call a version of Intent .putExtra() on the intent that is used in startActivity() .如果您想在创建 Activity 时将数据传递给它,请在startActivity()中使用的 Intent 上调用Intent .putExtra()的一个版本。 You can then use getIntent().getStringExtra() to (for example) get a string extra in the activity.然后,您可以使用getIntent().getStringExtra()来(例如)在活动中获取额外的字符串。

Say you have a piece of string data in your first activity called myString .假设您在第一个名为myString的活动中有一段字符串数据。

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(EXTRA_NAME_CONSTANT, myString);
startActivity(intent);

Now in your new activity in onCreate you would do:现在在onCreate的新活动中,您将执行以下操作:

String myString = this.getIntent()
        .getStringExtra(EXTRA_NAME_CONSTANT, "default return value here");

A few notes:几点注意事项:

  • For that EXTRA_NAME_CONSTANT , I do mean to make a string constant of the form "your.package.name.SomeString" for example "com.example.MyString".对于那个EXTRA_NAME_CONSTANT ,我的意思是制作一个形式为“your.package.name.SomeString”的字符串常量,例如“com.example.MyString”。 Personally I'd even use a resource (accessed in the form getString(R.string.extra_my_string) ) for the extra's name.就我个人而言,我什至会使用一个资源(以getString(R.string.extra_my_string)的形式访问)作为额外的名称。 They recommend you prefix it with your package name.他们建议您在其前面加上您的 package 名称。
  • You can put and get many types of data from strings to arrays to even serializable data.您可以从字符串到 arrays 甚至可序列化数据中放置和获取多种类型的数据。

Instead of making a separete activity for different layout orientations consider using resource qualifiers to provide alternative layouts .与其为不同的布局方向制作单独的活动,不如考虑使用资源限定符来提供替代布局

To summarize, make two layouts in a structure like so:总而言之,在一个结构中制作两个布局,如下所示:

/res/layout/yourlayout.xml
/res/layout-land/yourlayout.xml

Where both XML files are named the same.其中 XML 文件的名称相同。 Then make your default portrait layout in one and a landscape version in the other.然后在一个中制作默认的纵向布局,在另一个中制作横向版本。

When you inflate the layout in onCreate (and when it does so automatically on a layout change during runtime) it will select the correct layout for you.当您在onCreate中扩展布局时(并且当它在运行时布局更改时自动这样做时)它将 select 为您提供正确的布局。

I want to pass a fragment from the main activity to the new one, how would I do this?我想将主要活动中的片段传递给新活动,我该怎么做?

You wouldn't.你不会的。 At most, you would follow @Ribose's answer -- pass a flag into the activity via an extra to indicate what set of fragments to create.最多,您会遵循@Ribose 的回答——通过额外的将标志传递到活动中,以指示要创建的片段集。

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

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