作为序言,我有一个包含4个活动的Android应用程序,我希望将这些活动转换为一个包含片段的活动(两个扩展ListActivity
,两个扩展Activity
),导航抽屉启用片段之间的导航。我尝试将这些活动转换为扩展ListFragment
和Fragment
的活动,但是活动中的大部分代码都不再起作用:例如,getSystemService(NOTIFICATION_SERVICE)
、unbindService
和'registerReciever',主要是包含在不同活动中的不同onCreateOptionsMenu
。
因此,我问,是否有可能将任何单独的活动移植到一个零碎的活动中,但仍然保留与单独的重点活动相同的功能,而且编辑很少?
另外,关于转换过程,是否需要在主活动中结束以前的片段以在相同的空间中显示另一个片段?
发布于 2013-11-07 08:59:22
是的,有可能,片段的生命周期与活动(看这里)非常相似。您会发现,这可能是一种将活动类复制/粘贴到片段类的情况,只需进行非常小的调整,就可以使它们作为单独的活动工作。你唯一需要做的就是把这些片段从你的活动中交换出来。
例如,一个具有其onCreate函数的活动如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Find views
homeMessagesButton = (Button) v.findViewById(R.id.homeMessagesButton);
homeCreateMatchButton = (Button) v.findViewById(R.id.homeCreateMatchButton);
homeMyMatchesButton = (Button) v.findViewById(R.id.homeMyMatchesButton);
homeMyTeamsButton = (Button) v.findViewById(R.id.homeMyTeamsButton);
homeSquadButton = (Button) v.findViewById(R.id.homeSquadButton);
homeSettingsButton = (Button) v.findViewById(R.id.homeSettingsButton);
// Set click listeners
homeMessagesButton.setOnClickListener(this);
homeCreateMatchButton.setOnClickListener(this);
homeMyMatchesButton.setOnClickListener(this);
homeMyTeamsButton.setOnClickListener(this);
homeSquadButton.setOnClickListener(this);
homeSettingsButton.setOnClickListener(this);
}
现在,在片段中将类似于这样(注意,片段使用onCreateView生命周期方法在片段将出现的活动中膨胀其视图)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_home, container, false);
// Find views
homeMessagesButton = (Button) v.findViewById(R.id.homeMessagesButton);
homeCreateMatchButton = (Button) v.findViewById(R.id.homeCreateMatchButton);
homeMyMatchesButton = (Button) v.findViewById(R.id.homeMyMatchesButton);
homeMyTeamsButton = (Button) v.findViewById(R.id.homeMyTeamsButton);
homeSquadButton = (Button) v.findViewById(R.id.homeSquadButton);
homeSettingsButton = (Button) v.findViewById(R.id.homeSettingsButton);
// Set click listeners
homeMessagesButton.setOnClickListener(this);
homeCreateMatchButton.setOnClickListener(this);
homeMyMatchesButton.setOnClickListener(this);
homeMyTeamsButton.setOnClickListener(this);
homeSquadButton.setOnClickListener(this);
homeSettingsButton.setOnClickListener(this);
return v;
}
更改活动中的片段是通过使用fragmentTransaction管理器完成的,如android培训站点这里所示。
因为您的服务需要绑定到活动,而不是片段。这变得很简单,因为片段可以检索它们绑定到的活动。例如,在片段中使用的下面的代码将自动获得它们存在的活动。
getActivity().getSystemService(Context.LOCATION_SERVICE);
发布于 2013-11-07 08:54:06
将代码从活动中移开相当简单。例如,onCreateOptionsMenu也可以在片段中使用,对于需要上下文或活动的方法,可以从片段中调用getActivity()以获得对父活动的引用。
编辑:要替换一个片段,还有来自FragmentTransaction类的FragmentTransaction,在参数中使用容器视图的id。
发布于 2013-11-07 09:06:13
如果要在片段中使用getSystemService(NOTIFICATION_SERVICE),则需要使用活动的上下文。Context.getSystemService(NOTIFICATION_SERVICE).有些方法用于活动,如果您想在其他地方使用它们,则需要获得上下文。
https://stackoverflow.com/questions/19841761
复制