如何从片段本身替换活动的片段? [英] How to replace the activity's fragment from the fragment itself?

查看:125
本文介绍了如何从片段本身替换活动的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序都有其活动里面的片段。我想从目前的片段本身另一个编程方式替换片段。

My application has a Fragment inside its Activity. I would like to programmatically replace the fragment by another one from the current fragment itself.

例如,如果我点击片段内的一个按钮,该片段应用另一个替换,但活性应保持相同。

For example, if I click on a button inside the fragment, the fragment should be replaced with another one, but the activity should remain the same.

这可能吗?如果是的话,该怎么办呢?

Is it possible? If so, how to do it?

推荐答案

这其实很容易调用活动来代替片段。

It's actually easy to call the activity to replace the fragment.

您需要转换getActivity():

You need to cast getActivity():

((MyActivity) getActivity())

然后就可以调用从MyActivity方法,例如:

Then you can call methods from MyActivity, for example:

((MyActivity) getActivity()).replaceFragments(Object... params);

当然,这是假设你有一个replaceFragments()方法,在你的活动处理该片段取代过程。

Of course, this assumes you have a replaceFragments() method in your activity that handles the fragment replace process.

这篇关于如何从片段本身替换活动的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆