【问题标题】:How to save and load presets of Android activity?如何保存和加载 Android 活动的预设?
【发布时间】:2015-03-25 14:42:08
【问题描述】:

我有一个由多个容器(更具体地说是 FrameLayouts)组成的 Activity,用于显示片段。这些片段实际上是音频效果,所以一旦我选择了一些片段在它们相应的容器中显示并调整它们的旋钮(典型的吉他效果东西),我想保存并加载它们的预设。

换句话说,我想将我的 Activity 的视图(包含哪个效果在哪个容器中的数据,旋钮是什么值等)保存到一个文件中,然后加载该文件。加载时,我希望我的 Activity 切换到我保存在文件中的视图。

我已经制作了保存和加载的界面,但是我找不到保存和加载视图的方法。我发现了许多保存和加载方向更改视图的示例,但没有一个保存和加载文件的示例。

这是我用于保存和加载的代码的 sn-ps,太糟糕了,它不起作用。实际上,这只是我通过在互联网上找到的一些边际说明“糊涂”的,也许我离答案太远了:

public void save_file (Object file, String fileName, Context context)  {
    try {
        FileOutputStream fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(MainActivity.this);
        oos.writeObject(file);
        oos.flush();
        oos.close();
        fos.close();
        if (fileName != null) {
            Toast.makeText(context, "Save successful", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "Enter name", Toast.LENGTH_LONG).show();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static MainActivity load_file(String fileName, Context context) {

    MainActivity Main = null;
    try {
        FileInputStream fis = context.openFileInput(fileName);
        ObjectInputStream is = new ObjectInputStream(fis);
        Main = (MainActivity) is.readObject();
        is.close();
        fis.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return Main;
}

我也不知道如何将视图切换到加载的视图。感谢您的帮助。

【问题讨论】:

    标签: android android-fragments view save load


    【解决方案1】:

    所以现在看起来你所做的只是在 load_file 中创建一个新的 MainActivity 实例,但这对你没有任何好处。在 Android 中,您无需处理创建 Activity 类的实例,系统会为您完成。

    在我看来,对于您正在尝试做的事情,您应该使用共享首选项来存储有关您的视图的信息,然后使用 MainActivity 的 onCreate 方法中的所有正确值重新渲染视图。如果您不熟悉 SharedPreferences,可以查看此处的文档:http://developer.android.com/reference/android/content/SharedPreferences.html

    基本上,它只是像你已经在做的那样写入一个文件,但抽象了一点,这样更方便。你想在 MainActivity 中有一个名为 mPrefs 的字段,像这样在 onCreate 中初始化它:

    mPrefs = getSharedPreferences(<unique name here>, Context.MODE_PRIVATE);
    

    例如,假设用户将吉他旋钮调整为 6 的张力(我想象它会存储为 int)

    SharedPreferences.Editor editor = mPrefs.edit();
    //you should actually store the key string "guitar_knob_1" as a static final global variable rather than hard-coding
    editor.putInt("guitar_knob_1", 6);
    editor.commit();
    

    现在,回到 onCreate,你可以有这样的东西:

    if(mPrefs.getInt("guitar_knob_1", -1) != -1) { //-1 is the value we get if nothing is stored there
      //set the knob to the value we read
    }
    

    对我来说,按照您的要求进行操作的方式是存储与每个 FrameLayout 关联的视图名称的字符串,然后编写一些函数以在 onCreate 中呈现正确的视图。您实际上不需要“保存”视图,只需保存与它们关联的所有值并在活动打开时正确重建它们。希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多