android:如何在onResume时恢复对象,并在onPause时保存它?

发布于 2024-11-16 09:14:42 字数 152 浏览 4 评论 0原文

我想在暂停时保存一个对象(Myclass),并在应用程序恢复时加载它。

我尝试了一切,但没有任何效果。

我正在尝试创建一个线程并使其在问题出现时在我的主要活动上运行。 当我按下后退按钮(退出应用程序)并再次单击应用程序时,会创建一个新线程并且不会恢复旧线程。

I want to save a object(Myclass) on pause, and load it when the application resumes.

I tried everything but nothing works.

I'm trying to make a thread and make it run on my main activity when the problem comes.
When I press the back button(exit the application) and click again on the application, that creates a new thread and does not resume the old thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半夏半凉 2024-11-23 09:14:42

使类(MyClass)实现Serialized,然后您可以在活动销毁时将其保存为文件,并在恢复时从文件中恢复它

public boolean ObjectToFile(String fileName, Object obj){
    boolean success = false;        
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try{
        File dir = GetAppDir();
        if(dir != null){
            fos = new FileOutputStream(dir + "/" + fileName);
            out = new ObjectOutputStream(fos);
            out.writeObject(obj);
            out.close();
            success = true;
        }
    }catch(Exception e){}
    return success;
}

public Object FileToObject(String fileName){
    Object obj = null;
    try{
        File dir = GetAppDir();
        if(dir != null){
            File f = new File(dir, fileName);
            if(f.exists() && f.isFile()){       
                FileInputStream fis = null;
                ObjectInputStream in = null;
                fis = new FileInputStream(f);
                in = new ObjectInputStream(fis);
                obj = in.readObject();
                in.close();
            }
        }
    }catch(Exception e){}
    return obj;
}

Make the class (MyClass) implement Serializable, you can then save it as a file when the activity is destroyed, and restore it from file when it's resumed

public boolean ObjectToFile(String fileName, Object obj){
    boolean success = false;        
    FileOutputStream fos = null;
    ObjectOutputStream out = null;
    try{
        File dir = GetAppDir();
        if(dir != null){
            fos = new FileOutputStream(dir + "/" + fileName);
            out = new ObjectOutputStream(fos);
            out.writeObject(obj);
            out.close();
            success = true;
        }
    }catch(Exception e){}
    return success;
}

public Object FileToObject(String fileName){
    Object obj = null;
    try{
        File dir = GetAppDir();
        if(dir != null){
            File f = new File(dir, fileName);
            if(f.exists() && f.isFile()){       
                FileInputStream fis = null;
                ObjectInputStream in = null;
                fis = new FileInputStream(f);
                in = new ObjectInputStream(fis);
                obj = in.readObject();
                in.close();
            }
        }
    }catch(Exception e){}
    return obj;
}
栀子花开つ 2024-11-23 09:14:42

本质上你不能(也不应该)做你想做的事。如果您希望在用户结束会话*后继续执行代码,则您的代码应与服务关联运行。然后,代码应该将其工作写入持久存储,或者您的服务应该允许新创建的活动与线程连接的某些绑定接口。

*如果用户支持我们的活动,会话就会结束。这与用户按 HOME 键的行为不同,后者表明用户希望在返回应用程序时恢复他们正在执行的操作。

Essentially you can't (and shouldn't) do what you're trying to do. If you have code that you want to continue to execute after the user ends a session*, your code should be running in association with a Service. The code should then be writing its work to persistent storage or your Service should allow for some binding interface for the newly created Activity to connect with the Thread.

*A session ends if a user backs our of your Activities. This is a different behavior than the user pressing the HOME key, which indicates that the user wants to resume what they were doing when they return to your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文