将数据从Fragment移动到Activity在后台进行。

huangapple go评论67阅读模式
英文:

Move data from Fragment to Activity in the back

问题

我在我的Activity中有一个ViewPager。我需要从ViewPager中的片段中返回结果给Activity。同时,返回的数据将添加到Activity中的列表中。所以我认为Activity的示例不应该消失。我不知道是否应该使用Bundle来实现它。将对象与单例一起移动是否是一个好的解决方案?朋友们,这方面有什么最佳实践吗?

非常感谢您的回复。我猜想我可能没有清楚地表达我的问题。实际上,在CameraActivity中的ViewPager包含CameraFragment和GalleryFragment。这意味着不止一个Activity可以调用CameraActivity。CameraFragment或GalleryFragment应该能够将拍摄或选择的照片返回给调用CameraActivity的Activity / Fragment,该Activity / Fragment的类型为"File"。

英文:

I have a ViewPager in my Activity. I need to return the result from the fragments in the ViewPager to the activity. At the same time, the returned data will be added to the list in the activity. So I think the example of activity should not disappear. I don't know if I do it with Bundle. Would it be a good solution to move a singleton with object? What's the best practice for this, friends?

Thank you very much for your reply. I guess I may not have written my question clearly. In fact, the ViewPager in a CameraActivity has CameraFragment and GalleryFragment. This means that more than one activity can call CameraActivity. CameraFragment or GalleryFragment should be able to return the captured or selected photo to Activity / Fragmet, which calls CameraActivity of the "File" type.

答案1

得分: 1

在你的片段中添加一个接口,并通过接口传递数据。

class FragmentCategory : Fragment() {
    private lateinit var delegate: Interaction

    interface Interaction {
        fun onSelect(testParam: String)
        fun onShowLoading()
        fun onHideLoading()
    }

    companion object {
        fun newInstance(delegate: Interaction): FragmentCategory {
            val frag = FragmentCategory()
            val args = Bundle()
            frag.arguments = args
            frag.delegate = delegate
            return frag
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(
            R.layout.fragment_category_main,
            container, false
        )

        // 例如,你可以使用 delegate.onSelect("test") 来传递数据到你的活动
        return view
    }
}

在使用该片段的地方:

val frgCategory = FragmentCategory.newInstance(object : FragmentCategory.Interaction {
    override fun onSelect(testParam: String) {
        // 在这里处理选择操作
    }

    override fun onShowLoading() {
        // 在这里处理显示加载的操作
    }

    override fun onHideLoading() {
        // 在这里处理隐藏加载的操作
    }
})
英文:

add an interface to your fragment and pass your data with that.

class FragmentCategory : Fragment() {
 private lateinit var delegate: Interaction

 interface Interaction {
        fun onSelect(testParam:String)
        fun onShowLoading()
        fun onHideLoading()
    }

 companion object {
        fun newInstance(delegate: Interaction): FragmentCategory {
            val frag = FragmentCategory()
            val args = Bundle()
            frag.arguments = args
            frag.delegate = delegate
            return frag
        }

    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(
            R.layout.fragment_category_main,
            container, false
        )

         //for example you can use delegate.onSelect("test") for pass data to your activity 
         return view
    }
}

using that fragment:

val frgCategory= FragmentCategory.newInstance(object : FragmentCategory.Interaction {
            override fun onSelect(testParam:String) {
               
            }

            override fun onShowLoading() {
               
            }

            override fun onHideLoading() {
               
            }

        })

答案2

得分: 0

这是您可以做的事情(假设您想传回长整型值):

定义一个用于在ViewPager的片段和活动之间进行通信的接口

public interface CommunicationInterface {
    public void onDataReady(long value);
}

让您的活动实现这样的接口。

public class MainActivity extends AppCompatActivity implements CommunicationInterface {
    @Override
    public void onDataReady(long value) {
        // 从片段接收到的数据。
        // 在这里,您可以将值放入列表并显示它。
    }
}

在片段中,确保通过onActivityCreated回调获取父活动的引用:

public class FragmentTest extends Fragment {

    private CommunicationInterface parentActivity;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (CommunicationInterface) getActivity();
    }
}

现在,当您想要与活动进行通信时,只需使用:

parentActivity.onDataReady(3L);

确保在onActivityCreated回调之后不再调用parentActivity(只需检查它不为null)。

希望这有所帮助。

英文:

Here's what you can do (assuming you want to pass back long value):

Define an interface that will be used to communicate between ViewPager's fragments and the activity

public interface CommunicationInterface {
    public void onDataReady(long value);
}

Let your activity implement such interface.

public class MainActivity extends AppCompatActivity implements CommunicationInterface {
    @Override
    public void onDataReady(long value) {
        // data received from the fragments.
        // here you can put the value into the list and display it
    }
}

On the fragments, make sure to get the parent activity reference by getting it on the onActivityCreated callback:

public class FragmentTest extends Fragment {

    private CommunicationInterface parentActivity;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (CommunicationInterface) getActivity();
    }
}

Now, when you want to communicate back to the activity, just use

parentActivity.onDataReady(3L);

making sure that parentActivity is not called after onActivityCreated callback (just check that is not null).

Hope this helps.

huangapple
  • 本文由 发表于 2020年1月6日 15:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定