如何从恢复的onPause活动状态(onSavedInstanceState不会帮助) [英] How to restore activity state from onPause(onSavedInstanceState won't help)

查看:234
本文介绍了如何从恢复的onPause活动状态(onSavedInstanceState不会帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的应用程序有2 activities.SayA和B。一个启动B点。

活动B播放音乐(在服务),也有一个通知。

在该视图上的活动B和I preSS后退按钮,然后我就通知,活动B被打开与活动完全丧失和音乐的状态,单击也停止(不希望)... ...我想提出一个猜测,这是因为当我preSS后退按钮的活动被显式销毁,所以我必须progamatically恢复其观点吧??

该进入脑海的

的第一件事就是用onSavedInstanceState

但不会帮助,因为onSavedInstanceState不叫当用户presses后退按钮(我的文档阅读它),所以我有哪些替代方案?


解决方案

最简单的方法是保存在的onPause数据(),并在 onResume()。接下来的问题是在哪里存储数据。该是做几个方式:


  1. 使用自定义应用程序上下文

您可以扩展 android.app.Application

注册在你的清单文件类

 <应用机器人:名字=your.application.class...

这将允许您通过调用来获得你的类的单一实例`Context.getApplicationContext()。

举一个例子,你可以创建

 公共类MyContext扩展应用{
捆绑mySavedData = NULL;公共无效setSavedData(数据包){
    mySavedData =数据;
}公共捆绑getSavedData(){
    返回mySavedData;
}
}

,然后使用它像这样

  @overide
公共无效onResume(){
    ...
    束状态=((MyContext)getApplicationContext())getSavedData()。
    如果(状态!= NULL){
        / *恢复状态* /
    }
    ...
}@overide
公共无效的onPause(){
    ...
    束状态=新包();
    ...
    / *保存你的数据在这里和将其保存到上下文或无效的设置,否则* /
    ((MyContext)getApplicationContext())setSavedData(州)。
    ...
}


  1. 使用Singleton模式

而不是定义背景下,您可以创建一个实例的

 公共类MySingleton {
    静态MySingleton实例;    公共静态MySingleton的getInstance(){
        如果(例如== NULL){
            例如=新MySingleton();
        }
        返回实例;
    }    公共捆绑mySavedData = NULL;
    无效setSavedData(数据包){
        mySavedData =数据;
    }    公共捆绑getSavedData(){
        返回mySavedData;
    }
}

和您可以使用它

  @overide
公共无效onResume(){
    ...
    束状态= MySingleton.getInstance()getSavedData()。
    如果(状态!= NULL){
        / *恢复状态* /
    }
    ...
}@overide
公共无效的onPause(){
    ...
    束状态=新包();
    ...
    / *保存你的数据在这里和将其保存到上下文或无效的设置,否则* /
    。MySingleton.getInstance()setSavedData(州);
    ...
}

注意这种情况下,如果应用程序被杀害单身被破坏。如果你想永久保存你的数据比使用申请文件的文件夹或数据库,如果建议,但我认为这是不是你在找什么。

我希望这会帮助你....

basically my app has 2 activities.Say "A" and "B" . A launches B.

Activity B plays music(in a service) and also has a notification.

when the view is on activity B and i press back button ,and then i click on the the notification, activity B is opened with the state of activity completely lost and music also stops(not desired)......i am making a guess that it happens because when i press the back button the activity is explicitly destroyed ,so i have to progamatically restore its view right??

first thing that comes into mind is using onSavedInstanceState

but that won't help because onSavedInstanceState is not called when user presses back button (i read it in the docs) so what are my alternatives??

解决方案

The easiest way is to save data in onPause() and restore it in onResume(). Next question is where to store data. The are few way to do that:

  1. Using custom application context

You can extend android.app.Application and register your class in your manifest file by

<application android:name="your.application.class" ...

This will allow you to get singleton instance of your class by calling `Context.getApplicationContext().

For an example you can create

public class MyContext extends Application {
Bundle mySavedData = null;

public void setSavedData(Bundle data){
    mySavedData = data;
}

public Bundle getSavedData() {
    return mySavedData;
}
}

and then use it like this

@overide
public void onResume(){
    ...
    Bundle state = ((MyContext) getApplicationContext()).getSavedData();
    if(state != null) {
        /* restore states */
    }
    ...
}

@overide
public void onPause(){
    ...
    Bundle state = new Bundle();
    ...
    /* save your data here and save it into the context or set null otherwise*/
    ((MyContext) getApplicationContext()).setSavedData(state);
    ...
}

  1. Using singleton pattern

Instead of defining context you can create singleton instance

public class MySingleton {
    static MySingleton instance;

    public static MySingleton getInstance() {
        if(instance == null){
            instance = new MySingleton();
        } 
        return instance;
    }

    public Bundle mySavedData = null;
    void setSavedData(Bundle data){
        mySavedData = data;
    }

    public Bundle getSavedData() {
        return mySavedData;
    }
}

and you can use it

@overide
public void onResume(){
    ...
    Bundle state = MySingleton.getInstance().getSavedData();
    if(state != null) {
        /* restore states */
    }
    ...
}

@overide
public void onPause(){
    ...
    Bundle state = new Bundle();
    ...
    /* save your data here and save it into the context or set null otherwise*/
    MySingleton.getInstance().setSavedData(state);
    ...
}

Be aware that context and singletons are destroyed if application is killed. If you want to store your data permanently than using application document folder or database if recommend but I think that isn't what you looking for.

I hope this will help you....

这篇关于如何从恢复的onPause活动状态(onSavedInstanceState不会帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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