【问题标题】:Communication between Activity and FragmentActivity 和 Fragment 之间的通信
【发布时间】:2015-03-12 09:54:00
【问题描述】:

大家好,我正在开发一个应用程序,为了解释我的情况,我创建了一个简单的插画家。当我单击另一个活动中的按钮时,我想将数据发送到片段。我使用 bundle 它给了我空指针异常。我试图在另一个活动中使用方法,它再次给了我错误。我如何将数据从 Activity 传递到 Fragment 提前谢谢。

这是我的代码。

Edit1 : 我想我解释错了。看看我上传的照片。当我单击白色按钮时,另一个活动正在打开。当我在另一个活动中输入一些文本并单击另一个活动中的按钮时,我想发送数据片段类。我希望我能够解释。谢谢。

MainActivity

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button) findViewById(R.id.button2);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,AnotherActivity.class);
                startActivity(i);

            }
        });
    }
}

另一个活动

public class AnotherActivity extends Activity {
    EditText et;
    Button btn;
    public String ets;
    FragmentClass myFragment = new FragmentClass();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anotheractivity);
        btn = (Button) findViewById(R.id.button);
        et= (EditText) findViewById(R.id.editText);
        ets = et.getText().toString();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myFragment.Test(ets);

            }
        });
    }
}

片段

public class FragmentClass extends Fragment {
    TextView tv;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment,container,false);
        tv = (TextView)v.findViewById(R.id.textView);

        return v;
    }


    public void Test(String name){
       Toast.makeText(getActivity(),name,Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

  • 片段附加到哪个活动。在图像中它附加到 MainActivity
  • 是的,片段附加到主活动,我想将数据从另一个活动发送到片段。 (不是:主要活动片段)
  • 你可以使用startActivityForResult覆盖onActivityResult
  • 你也可以使用第三方库,比如greenrobots eventbus
  • 我无法理解您的问题。你想在 2 个活动之间交流信息,并且你想在一个片段中使用它。我说的对吗?

标签: java android android-fragments


【解决方案1】:

您可以将不同的数据放入 Bundle 中(我在下面的示例中传递了一个对象和一个字符串),也可以使用 FragmentManager 和 FragmentTransaction。在 AnotherActivity 的 onClick 中尝试这样的操作:

            Bundle bundle = new Bundle();
            bundle.putParcelable("SomeObject", obj);
            bundle.putString("attribute", "some string value");
            otf.setArguments(bundle);

            fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.container, myFragment, "MYFRAGMENT_NAME");
            ft.addToBackStack("MYFRAGMENT_NAME");
            ft.commit();

然后在新片段中使用getArguments() 从Bundle 中获取数据。

【讨论】:

  • 感谢您的评论,但 myfragment 类已添加到地图上。我不需要添加片段交易。我错了吗?
  • 我会推荐使用 FragmentManager 和 FragmentTransaction。根据官方文档developer.android.com/guide/components/fragments.html,仅调用片段类不是正确的方法
【解决方案2】:

此页面上的其他答案都包含您在完全了解活动和片段如何工作后可以使用的答案片段,但在我看来,您似乎还不了解它们。

有很多很好的资源,包括书籍和网站,你可以从中学习,但我怀疑这里有一些基础知识会让你感到困惑:

  • 在任何给定时间只有一个活动处于活动状态。

  • 活动有一个“生命周期” - 一系列从完全不活动到完全活动的状态,以及在达到每个状态时调用的标准方法。

  • Fragment 必须附加到 Activity。

  • 片段的活动生命周期类似于(但不完全相同)活动。

  • 您不能安全地从另一个 Activity 调用一个 Activity 中的方法。如果你想在两个Activity之间传递数据,你通常通过一个Intent来完成。

  • 您不能在一个 Activity 或 Fragment 与另一个 Activity 或另一个 Activity 中的 Fragment 之间直接通信。如果从您的问题中可以看出,您希望将 Activity 中的数据传递给另一个 Activity 中的 Fragment,则必须将数据传递给 Intent 中的另一个 Activity,然后让该 Activity 将其传递给它的 Fragment生命周期中的适当点。

【讨论】:

    【解决方案3】:

    这是在 Activity 和 Fragment 之间进行通信的最佳做法

    public class TestFragment extends Fragment {
    
        private static final String ATAG = "atag";
    
        public static TestFragment getFragment(int value){
            TestFragment fragment = new TestFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(ATAG, 100);
            fragment.setArguments(bundle);
    
            return fragment;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            View view  = inflater.inflate(R.layout.layout_fragment, container,false);
            Bundle  b = getArguments();
                if(b!= null)
                    YOUR DATA = b.getInt(ATAG);
            return view;
        }
    }
    

    还有活动:

    public class MainActivity extends Activity{
    
        private static final String FOO = "foofragment";
    
        TestFragment newFragment;
        Button mButton;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if(savedInstanceState == null){
                newFragment = TestFragment.getFragment(100);    
            }else {
                newFragment = (TestFragment) getFragmentManager().findFragmentByTag(FOO);
            }
    
            mButton.setOnClickListener(new OnClickListener() {
                FragmentManager manager = getFragmentManager();     
                FragmentTransaction transaction = manager.beginTransaction();   
                transaction.add(R.id.container, newFragment, FOO);          
                transaction.commit();
            }
        }
    }
    

    【讨论】:

    • 感谢您的评论。我的片段已添加到地图中,但您建议我使用片段事务。当我启动应用程序时,片段会正常出现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多