内部DialogFragment类是否应该是静态的? [英] Should an internal DialogFragment class be static or not?

查看:104
本文介绍了内部DialogFragment类是否应该是静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来学习Android的项目代码片段:

This is a snippet of code from my project that I am using to learn Android:

private void enableLocationSettings() {
    Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(settingsIntent);
}

@SuppressLint("ValidFragment")
public class EnableGpsDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getActivity())
            .setTitle("Tytuł")
            .setMessage("wiadomosc")
            .setPositiveButton("odpal", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    enableLocationSettings();
                }

            })
            .create();
    }
} 

如您所见,我必须添加@SuppressLint才能使我的应用正常运行,但是要在指南不需要此注释.

As you can see I must add @SuppressLint to make my app work but on the guide this annotation wasn't necessary.

我在做什么错了?

这是我的进口货

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

推荐答案

该示例没有这些注释,因为该类位于其自己的文件中.这意味着使用片段的是独立的.

The example doesn't have those annotations because the class is in its own file. This means it is independent of the Activity that uses the Fragment.

在您的情况下,您的Fragment在Activity内,并且不使用static修饰符.这意味着它与Activity实例相关联.

In your case, your Fragment is inside an Activity and doesn't use the static modifier. This means it is tied to the Activity instance.

让Fragment依赖Activity实例是一个坏主意,这两个类都具有复杂的生命周期(尤其是因为Activity经常被破坏和重新创建),并且应该彼此独立.

Having the Fragment depend on the Activity instance is a bad idea, both these classes have complex lifecyles (especially since Activities get destroyed and recreated quite often) and should be independent of each other.

您将需要使EnableGpsDialogFragment的修饰符static.

public static class EnableGpsDialogFragment extends DialogFragment {

static类不依赖于封闭类的实例,因此警告将消失.

A static class does not depend on the enclosing class' instance, so the warning will go away.

有关更多信息,请阅读嵌套类上的Java教程.

For more information, read the Java tutorial on nested classes.

根据您的编辑进行 现在,这些类不再依赖于彼此的实例,您将必须获取YourActivity的实例,以便可以通过铸造方式调用enabledLocationSettings(),并且仅当EnableGpsDialogFragment仅由<使用时才能使用c4>:

Edit in response to your edit: Now that the classes don't depend on each other's instance, you will have to get out an instance of YourActivity so you can call enabledLocationSettings() one way is by casting and will only work if EnableGpsDialogFragment is only used by YourActivity:

@Override
public void onClick(DialogInterface dialog, int which) {
  enableLocationSettings();
}

@Override
public void onClick(DialogInterface dialog, int which) {
  ((YourActivity)getActivity()).enableLocationSettings();
}

如果此片段将由多个活动使用,则应创建要由以下对象实施的interface而不是每个活动.

If this Fragment will be used by multiple Activities, you should create an interface to be implemented by each Activity instead.

这篇关于内部DialogFragment类是否应该是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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