android - Fragments 和 Activity——我应该把我的应用程序逻辑放在哪里?

标签 android android-fragments

我已经创建了我想要的 View 。它有 1 个图像、一个输入框和一个按钮。单击按钮时,我将要加载另一个 Activity 。我很困惑为什么会有 fragment 和 Activity 。我是 Android 世界的新手(来自 iOS)。

我的理解是 Activity 类似于 ViewControllers,但我不确定我是否理解 fragment 是什么。

我应该把事件处理放在哪里?

package com.phppointofsale.phppointofsale;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class StoreUrlActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_url);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new StoreUrlFragement()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.store_url, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class StoreUrlFragement extends Fragment {

        public StoreUrlFragement() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_store_url,
                    container, false);
            return rootView;
        }
    }

}

最佳答案

首先,我建议您阅读这篇 Fragments .请特别注意创建的 fragment 部分,其中包括 fragment 生命周期图。第二次下载编译这个Sample App ,有效的导航应用程序将帮助您了解不同的 fragment 如何协同工作,甚至可以实现操作栏。

要或多或少地回答您的问题,可以将 fragment 视为一个单独的类。调用该特定 fragment 后,您可以从该类中调用函数。

fragment 大小写转换

这是一些示例代码来向您展示我的意思。

 public Fragment getItem(int i){
    switch (i) {
        case 0:
            // The first section of the app is the most interesting -- it offers
            // a launchpad into the other demonstrations in this example application.
            return new LaunchpadSectionFragment();

        case 1:
            return new BluetoothClass();
      
        default:
            // The GPS section of the app .
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            return fragment;
        }
}

在这种情况下,每个 fragment 对我来说都代表一个类,它在单独的选项卡中实现,并且每个选项卡都有单独的功能。 fragment 的主要优势之一是您可以运行单独的 Activity ,而无需先让一个 Activity 完成。

此外,每个 fragment 都是 java.lang.Object 库的扩展。所以它具有所有这些功能 + 附加功能。我会读 this以及。最后,最好为每个 fragment 使用单独的 xml 文件,然后您可以在调用 fragment 时单独显示它。

更多代码

每个 fragment 都会/可能有这个

public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
            // Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners

        View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
        rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do something 
        }
    });
}
            
    
    public void onStart(){
        super.onStart();
        Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show();
    }       

    public void onResume(){
        super.onStart();
        Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show();
                
    }
    
    public void onStop(){
        super.onStart();
        Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show();
        disableBT();
    }

请记住,这些函数来 self 之前提到的 fragment 生命周期。

希望这能让您对 fragment 有所了解。还记得阅读 this因为许多功能使用 v7 应用程序兼容库。包括 fragment manager .

关于android - Fragments 和 Activity——我应该把我的应用程序逻辑放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290938/

相关文章:

java - 在Android中使用Zxing扫描仪如何读取UPI QR码?

android - 旋转时保留 Fragment 对象

java - addToBackStack() 不适用于 getChildFragmentManager()

android - Robotium 相当于 Espresso 中的 drag()?

java - Robotium - 如何截取屏幕截图并从测试用例内部打开

android - gradle中依赖项的动态版本(使用+)

java - android intent-filter <data> 无法正常工作

android - 如何通过焦点调整屏幕中两个 fragment 的大小?

Android 支持库 27.1.0 新方法 requireActivity()、requireContext()

java - fragment 中的 super.onCreateView