我们如何将上下文转换为片段引用? [英] How do we cast context to fragment reference?

查看:84
本文介绍了我们如何将上下文转换为片段引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用类和一个片段 FragmentTest。 Common.java是一个通用类,具有一些用于其他活动的通用功能。这些功能可以通过每个活动的上下文进行访问。在这里,我将 fragment的上下文传递给了那堂课我是这样的

I have a class 'Common' and a fragment 'FragmentTest'. The 'Common.java' is a general class that have some common functions for other activities..Those functions are accessed by context of each activities.. And here I am passing the fragment's context to a function in that class. I am doing like this

在片段中:-

 Common commonObj = new Common();
 commonObj.myfunction(this.getActivity(),"Do you want to Update ?");

在Class中,经过一些操作后,我试图返回片段类。就像
:-

And in Class after some operation i'm trying to return back to fragment class.Like this :-

public void myfunction(Context context , String str){    
   //....//
  if(context.getClass().isInstance(FragmentTest.class)){
      **FragmentTest mContext = (FragmentTest)context;**
      mContext.FunctionInFragment();
  }
}

但是我对此有误。因为我不能将上下文转换为片段引用。
有人请帮忙。

But i have error in this..Because i cannot cast the context to fragment reference. Somebody please help..

推荐答案

首先,您不能投射 Context Fragment ,因为 Fragment 不会扩展 Context 活动确实扩展了上下文,这就是为什么从活动中执行此操作时会起作用的原因。

Firstly you can't cast a Context to a Fragment as Fragment doesn't extend Context. Activity does extend Context which is why when you do this from an Activity what you are trying works.

理想情况下,我建议您在 Common 类中使用的方法应该完全不知道您的片段。这样,它们就不会耦合在一起。为此,您可以使用回调接口。

I'd suggest ideally that the methods in your Common class should be totally unaware of the existence of your Fragment. This way they are not 'coupled' together. To achieve this you can use a callback interface.

创建一个接口如下:

public interface Callback<T> {

    onNext(T result);
}

然后您可以在 Common 中更改方法。 code>到以下内容:

Then you can change your method in Common to the following:

public void myfunction(Callback<Void> callback , String str){    

    //....//
    callback.onNext(null);
}

然后在 Common中调用该方法片段中的code> ,您可以这样操作:

Then when you call the method in Common from the Fragment you would do it like this:

Common commonObj = new Common();
commonObj.myfunction(
    new Callback<Void>() {
        @Override
        public void onNext(Void result) {
            functionInFragment();
        }
    },
    "Do you want to Update ?"
);

如果需要将数据发送回该函数,则可以更改回调的返回类型。例如,如果您想传回一个字符串,则可以使用 Callback< String> ,然后原始调用中的方法将如下所示:

If you needed to send data back to the function then you can change the return type of the callback. For instance if you wanted to pass back a string you would use Callback<String> and then the method in the original call would look like this:

new Callback<String>() {
    @Override
    public void onNext(String result) {

    }
}

并在您的 Common 类,您将这样称呼它:

And in your Common class you would call it like this:

public void myfunction(Callback<String> callback , String str){    

    //....//
    String result = "Hello from common";
    callback.onNext(result);
}

这篇关于我们如何将上下文转换为片段引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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