简体   繁体   English

Android:在恢复应用程序时恢复活动的Activity

[英]Android: Restoring the active Activity while resuming the application

I have two activities running: mainActivity and childActivity . 我有两个运行的活动: mainActivitychildActivity Whenever the user clicks in the button in mainActivity , the childActivity is launched. 只要用户单击mainActivity中的按钮, 就会启动childActivity What I want to do is this: 我想要做的是:

When the active activity is the childActivity and the user clicks the home button then relaunch the application , I want to see the childActivity instead of mainActivity that is launched. 当活动活动是childActivity并且用户单击主页按钮然后重新启动应用程序时 ,我希望看到childActivity 而不是启动 mainActivity

I had some suggestions actually work arounds. 我有一些建议实际上解决了。 I tried to manipulate onStart, onRestart, onResume, onStop, onDestroy events. 我试图操纵onStart,onRestart,onResume,onStop,onDestroy事件。 But, they didn't fully solve the problem. 但是,他们没有完全解决问题。 There should be a smart way out there. 应该有一个聪明的方法。 Thank you. 谢谢。

Edit: 编辑:

Thank you for the answer, Soonil. 谢谢你的回答,Soonil。 The case you said is happening when the activity is called from recent activities window. 您说的是从最近的活动窗口调用活动时发生的情况。 (the window opened when you long press the home button) However; (长按主页按钮时窗口打开)但是; This is not happening when you open it from home screen. 从主屏幕打开时不会发生这种情况。 (like opening from start) I don't think my code has a specific problem to generate this error. (比如从头开始)我不认为我的代码有一个特定的问题来产生这个错误。 Because, I created a test project and tried standalone before sending the question and faced the same problem. 因为,我创建了一个测试项目并在发送问题之前单独尝试并面临同样的问题。 Anyhow, here is the test code: 无论如何,这是测试代码:

public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("MainActivity");

        ((Button) findViewById(R.id.btnChildActivity)).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        startActivity(new Intent(this, ChildActivity.class));
    }

}

public class ChildActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        this.setTitle("ChildActivity");

    }
}

EDIT : Found the solution to your problem somewhat randomly today! 编辑 :今天有点随机找到你的问题的解决方案! See this error report . 请参阅此错误报告 It explains your problem exactly. 它完全解释了你的问题。 The reason I couldn't reproduce the problem is I never have Eclipse launch an app directly. 我无法重现问题的原因是我从未让Eclipse直接启动应用程序。 I use Eclipse to install the app and then start it myself. 我使用Eclipse安装应用程序,然后自己启动它。


This is already the default behavior for Android applications, no special tricks are required to achieve this. 这已经是Android应用程序的默认行为,无需特殊技巧即可实现此目的。 I'm surprised your application isn't demonstrating this behavior. 我很惊讶您的应用程序没有证明这种行为。 Every Android application maintains an Activity stack, literally a LIFO stack of activities. 每个Android应用程序都维护一个Activity堆栈,实际上是LIFO堆栈的活动。 These activities can be further grouped into tasks, but 99% of mundane apps won't ever need to know anything about tasks in my experience. 这些活动可以进一步分为任务,但99%的普通应用程序根本不需要了解我的经验中的任务。

When you press the home button, the entire application stack is put into the background. 当您按下主页按钮时,整个应用程序堆栈将被放入后台。 While in the background, it may be killed for memory concerns at any time, but if not much time elapses before it is restored, it generally isn't killed and doesn't have to be recreated. 在后台,它可能会在任何时候因内存问题而被杀死,但如果在恢复之前没有多少时间过去,它通常不会被杀死而且不必重新创建。 When you select the application again, the stack (or more accurately, only the top item on the stack) is restored. 再次选择应用程序时,将恢复堆栈(或更准确地说,只是堆栈中的顶部项目)。

If your application isn't exhibiting this behavior, I suspect it has something to do with how you are starting the mainActivity and childActivity and any extra Intent flags you may be using. 如果您的应用程序没有表现出这种行为,我怀疑它与您如何启动mainActivity和childActivity以及您可能正在使用的任何额外的Intent标志有关。 Any chance you can post code snippets on how you are starting the mainActivity and childActivity? 你有没有机会发布关于如何启动mainActivity和childActivity的代码片段?

I went back and tested with a similar application, and even when the process is forced out of memory the ChildActivity is reconstituted automatically as Soonil says. 我回去并使用类似的应用程序进行了测试,即使进程被强制内存不足,也会像Soonil所说的那样自动重构ChildActivity。 Are you seeing this on the emulator or on an actual device? 您是在模拟器上还是在实际设备上看到这个?

If you run your app and watch logcat, you should see something like the following after you launch your App, then open the ChildActivity and click Home and then launch your activity again: 如果您运行应用程序并观察logcat,则应在启动应用程序后看到类似以下内容的内容,然后打开ChildActivity并单击Home,然后再次启动您的活动:

Starting activity: Intent { action=android.intent.action.MAIN categories={android.intent.categroy.LAUNCHER} flags=... comp={com.yourpackagename.MainActivity} } Start proc for activity yourpackagename.ChildActivity: pid=x uid=y gids={} Displayed activity /.ChildActivity 启动活动:Intent {action = android.intent.action.MAIN categories = {android.intent.categroy.LAUNCHER} flags = ... comp = {com.yourpackagename.MainActivity}}启动活动的proc yourpackagename.ChildActivity:pid = x uid = y gids = {}显示的活动/.ChildActivity

Could you post the output of logcat when you do not see the behavior you expect? 当你没有看到你期望的行为时,你能发布logcat的输出吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM