【问题标题】:how to get from a fragment to an activity without disturbing fragment's reausability如何从一个片段到一个活动而不干扰片段的可重用性
【发布时间】:2018-02-28 21:00:12
【问题描述】:

这让我很烦恼。

我想在不影响片段可重用性的情况下从片段中获取活动。这意味着我不能直接从片段内部启动一个 Intent 到该片段当前附加到的活动。

这是有问题的代码

//This is the code inside the fragment 

public void onStart() {
    super.onStart();
    View view = getView();

    switch (position){
        //case 1 is the case when the user clicks the ADD List Item from a ListFragment. 
        //Position is the position of ADD in the List Fragment.
        case 1:{
            Intent intent = new Intent(/*what exactly should I put here?*/  ,  
        /*this is where the reference to activity the fragment is attached to goes.
            But we dont know what activty the fragment is attached to, as it is reusable 
         and may get attached to different activity at different times*/);
        }

        case 2:{
            //this is the case when user decides to view the entered text in the array list.
            TextView textView = (TextView)view.findViewById(R.id.display_name);
            int size = workout.arrayList.size();
            Object[] array = workout.arrayList.toArray();
            for(int i=0;i<array.length;i++){
                textView.setText((String)array[i] + "\n");
            }
        }
    }

}

我觉得数据可能不足,虽然我不确定还能提供什么,对此感到抱歉。

如果需要更多数据,请告诉我。

【问题讨论】:

  • 你到底想要什么??干扰可重用性是什么意思..
  • 片段以其可重用性而闻名,为了做到这一点,片段和活动不应直接相互通信。您通过接口进行通信。由于片段可能与其他活动一起使用,因此这是必不可少的
  • 仍然没有得到你想要达到的目标??
  • 一种访问片段所附加到的活动的方法。
  • 参加活动是什么意思?打开父活动??从片段本身??

标签: java android android-activity fragment


【解决方案1】:

在 Fragment 中,我们使用以下方法获取 Intent 的上下文:

Intent intent = new Intent(getActivity(),AnyActivity.class);

Intent intent = new Intent(getView.getContext(),AnyActivity.class);

AnyActivity() 可以是任何活动,包括当前具有Fragment 的活动。

【讨论】:

  • 是的,这是我脑海中闪过的第一个想法。但是这样做的问题是片段会将意图传递给AnyIntent。如果相同的片段用于名称肯定不能是AnyActivity 的不同活动中会怎样。在这种情况下,我们需要获取fragment 当前附加到的活动。
  • 那么你的意思是这个Intent intent = new Intent(getActivity(),getActivity().getClass()); 告诉我你是不是这个意思,以便我可以更新我的答案。
  • 我其实不知道。但无论如何,感谢您的帮助。
【解决方案2】:

您可以像这样打开相同的活动..

        Intent intent = new Intent(getActivity(),SameActivity.class);

      intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

更新要将片段重用于不同的值,请将参数传递给片段......在事务期间......

      Bundle bundle = new Bundle();
      bundle.put("className", "SameActivity");
      fragment.setArguments(bundle);

在片段中从包中获取类名...不同的类将作为包传递不同的参数..

OtherActivity 包中看起来像这样..

      Bundle bundle = new Bundle();
      bundle.put("className", "OtherActivity");
      fragment.setArguments(bundle);

【讨论】:

  • 请参考我给Xenolion的回复。
  • 您正在使用getActivity() 获取附加片段的当前活动,这是当前活动的引用.. 现在有什么问题?? @adityarawat
  • 问题是意图被发送到SameActivity.class。该片段可以被不同的活动重用(例如SomeOtherActivity)。但是当片段的代码运行时,它总是将 Intent 发送到 SameActivity,但 Intent 应该发送到 SomeOtherActivity
  • 我已经更新了,请问您是否理解有困难.. @adityarawat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多