Android的 - 恢复上次浏览活动 [英] Android - restore last viewed Activity

查看:113
本文介绍了Android的 - 恢复上次浏览活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有特定的顺序之间的用户定位3个不同的活动。我的目标是双重的:

I have 3 different Activities that user navigates between in no particular order. My goal it twofold:

  1. 当用户切换到别的应用程序时,恢复我要开始的地方用户离开,即使应用程序被终止
  2. 当最后一个活动是恢复我想恢复到上次查看的状态(这个我想我有一个关于如何才达到一个pretty的好主意)

我觉得这个问题是不是开始/停止 - 在我pretty的多少得到我所需要的,但的onCreate()如果应用程序被终止。在这种情况下 - 它选择了我在清单配置的活动。我想我可以把东西在默认活动的onCreate方法,但有没有更好的方式,我可能丢失?

I think the problem is not start/stop - where I pretty much get what I need, but onCreate() if app was terminated. In that case - it picks Activity that I configured in the manifest. I suppose I can put something in onCreate method of that default activity but is there a better way that I'm maybe missing?

推荐答案

如果您的应用程序还没有被终止,那么#1应该已经工作,#2只需要节省尚未被管理的自动的进入包的任何值在<一个href="http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29">onSaveInstanceState()然后还原它们<一个href="http://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState%28android.os.Bundle%29">onRestoreInstanceState().

If your app hasn't been "terminated" then #1 should already work and #2 just requires saving any values that aren't managed automagically into the Bundle in onSaveInstanceState() then restoring them in onRestoreInstanceState().

这是怎样的一个黑客,但我觉得对#1你最佳的选择在实际上被终止会保存最新的活动,对于你的Activity类的onResume应用程序的话,那么当你第一次运行您的第一个活动的OnCreate做一次检查,然后启动正确的活动......甚至把一个空白的活动的开始。事情是这样的:

This is kind of a hack, but I think your best option for #1 in the case of the app actually being terminated would be to save the most recent Activity in the onResume of each of your Activity classes then when you first run the onCreate of your first activity do a check then start the correct Activity... maybe even put in a blank Activity at the beginning. Something like this:

StartActivity:

StartActivity:

public class StartActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // get last open Activity
        String lastActivity = PreferenceManager.getDefaultSharedPreferences(this).getString("last_activity", "");
        if (last_activity == MyActivity2.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity2.class));
        } else if (last_activity == MyActivity3.getSimpleName()) {
            startActivityForResult(new Intent(this, MyActivity3.class));
        } else {
            // assume default activity
            startActivityForResult(new Intent(this, MyActivity1.class));
        }
    }

    public void onActivityResult() {
        // kill the activity if they go "back" to here
        finish();
    }
}

然后,在所有其他活动(MyActivity1,2,3)保存的值,像这样:

Then in all the other Activities (MyActivity1,2,3) save the values like so:

@Override
public void onResume() {
    Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit();
    e.putString("last_activity", getClass().getSimpleName());
    e.commit();

    super.onResume();
}

你还必须处理保存/手动恢复数据的每个活动。你可以保存所有你需要到每一个活动的的onPause()里面的preferences数值,然后将它在onResume恢复()。

You'll also have to handle saving /restoring the data for each Activity manually. You could save all the values you need into the preferences inside the onPause() of each of the Activities then restore it in the onResume().

这篇关于Android的 - 恢复上次浏览活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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