【问题标题】:Turning multiple pre-created Android activities to a single activity with fragments将多个预先创建的 Android 活动转换为带有片段的单个活动
【发布时间】:2013-11-08 01:14:54
【问题描述】:

作为序言;我有一个包含 4 个活动的 Android 应用程序,我希望将这些活动转移到一个包含 Fragments 的活动中(两个扩展 ListActivity,两个扩展 Activity),通过 Navigation Drawer 启用 Fragments 之间的导航。我试图将这些活动变成扩展 ListFragmentFragment 的活动,但是活动中的大部分代码都停止运行:例如,getSystemService(NOTIFICATION_SERVICE)unbindService 和“registerReciever”,主要是不同的活动中包含不同的onCreateOptionsMenus。

因此我问,是否可以将任何单独的活动移植到单个碎片化的活动中,但仍保留与单独的重点活动相同的功能,并且只需最少的编辑?

另外,关于过渡过程,是否需要在主 Activity 中结束前一个 Fragment 以在同一空间显示另一个 Fragment?

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    是的,有可能,片段的生命周期与活动非常相似 (See here)。您会发现这可能是从您的活动类复制/粘贴到您的片段类的情况,只需进行非常小的调整即可让它们像单独的活动一样工作。您唯一要做的工作就是从您的活动中交换这些片段。

    例如,一个带有 onCreate 函数的 Activity 如下所示:

    @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 生命周期方法在片段将出现的 Activity 中膨胀其视图)

    @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;
    }
    

    在 Activity 中更改片段是通过使用 fragmentTransaction 管理器完成的,如 android 培训网站here 所示

    因为您的服务需要绑定到一个活动,而不是一个片段。这变得微不足道,因为片段可以检索它们绑定的活动。例如,片段中使用的以下代码将自动获取它们存在的活动。

    getActivity().getSystemService(Context.LOCATION_SERVICE);
    

    【讨论】:

      【解决方案2】:

      如果您想在片段中使用 getSystemService(NOTIFICATION_SERVICE),您需要使用活动的上下文。上下文.getSystemService(通知服务)。有些方法适用于活动,如果您想在其他地方使用它们,您需要获取上下文。

      【讨论】:

        【解决方案3】:

        从 Activity 移动代码非常简单。例如 onCreateOptionsMenu 也可以在 Fragments 中使用,对于需要上下文或 Activity 的方法,您可以从 Fragment 调用 getActivity() 以获取对父 Activity 的引用。

        编辑:要替换 Fragment,还有来自 FragmentTransaction 类的 a method,在参数中采用容器 View 的 id。

        【讨论】:

        • 有哪些 Android 函数,例如 bindService,当 Activity 被转换为片段时,会在 .class 中变得未定义?
        • 我不清楚您所说的“成为类未定义”是什么意思,但 bindService 是 Context 的一种方法,您也可以在 getActivity 上调用它...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多